• LeetCode 242


    Valid Anagram

    Given two strings s and t, write a function to determine if t is an anagram of s.

    For example,
    s = "anagram", t = "nagaram", return true.
    s = "rat", t = "car", return false.

    Note:
    You may assume the string contains only lowercase alphabets.

    Follow up:
    What if the inputs contain unicode characters? How would you adapt your solution to such case?

     1 /*************************************************************************
     2     > File Name: LeetCode242.c
     3     > Author: Juntaran    
     4     > Mail: Jacinthmail@gmail.com
     5     > Created Time: 2016年05月10日 星期二 03时49分39秒
     6  ************************************************************************/
     7 
     8 /*************************************************************************
     9     
    10     Valid Anagram
    11     
    12     Given two strings s and t, write a function to determine if t is an anagram of s.
    13 
    14     For example,
    15     s = "anagram", t = "nagaram", return true.
    16     s = "rat", t = "car", return false.
    17 
    18     Note:
    19     You may assume the string contains only lowercase alphabets.
    20 
    21     Follow up:
    22     What if the inputs contain unicode characters? How would you adapt your solution to such case?
    23 
    24  ************************************************************************/
    25 
    26 #include "stdio.h"
    27 
    28 int isAnagram(char* s, char* t)
    29 {
    30     int ret;
    31 
    32     if (strlen(s) != strlen(t))
    33     {
    34         ret = 0;
    35     }
    36 
    37 
    38     int i;
    39     int flag[26] = {0};
    40 
    41     for ( i=0; i<strlen(s); i++ )
    42     {
    43         flag[s[i] - 'a']++;
    44         printf("%d ",s[i] - 'a');
    45         printf("%d
    ",flag[s[i] - 'a']);
    46 
    47         flag[t[i] - 'a']--;
    48         printf("%d ",t[i] - 'a');
    49         printf("%d
    ",flag[t[i] - 'a']);
    50     }
    51     
    52     for( i=0; i<26; i++ )
    53     {
    54         if( flag[i] != 0 )
    55         {
    56             ret = 0;
    57         }
    58         printf("%d ",flag[i]);
    59     }
    60     ret = 1;
    61     printf("
    %d
    ",ret);
    62     
    63     return ret;
    64 }
    65 
    66 int main()
    67 {
    68     char* s = "nl";
    69     char* t = "cx";
    70     
    71     int result = isAnagram(s,t);
    72     
    73     return 0;
    74 }
  • 相关阅读:
    iOS
    WKWebview 获取网页加载内容的高度
    iPhoneXS、XS Max与iPhoneXR 适配
    iOS:iPhone X,iPhone XS,iPhone XR,iPHone XS Max 适配
    git---全局设置用户名、密码、邮箱
    iOS 拖动手势(UIPanGestureRecognizer)
    ios 扩大button的点击区域
    iOS总结 | 修改button响应区域的各种姿势
    swift常用第三方库
    《从零开始学Swift》学习笔记(Day 33)——属性观察者
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5479098.html
Copyright © 2020-2023  润新知