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\n",flag[s[i] - 'a']);
46 
47         flag[t[i] - 'a']--;
48         printf("%d ",t[i] - 'a');
49         printf("%d\n",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("\n%d\n",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 }

 

posted @ 2016-05-10 19:11  Juntaran  阅读(188)  评论(0编辑  收藏  举报