1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| /**
Time complexity: O(2nlogn)
Space: O(2n)
*/
- (BOOL) isAnagram1_4_2:(NSString *)s1
withString:(NSString *)s2
{
if (nil == s1 || nil == s2 || [s1 length] != [s2 length] || [s1 isEqualToString:s2]) return NO;
char string1[[s1 length] + 1];
memccpy(string1, [s1 UTF8String], sizeof(char), [s1 length] + 1);
char string2[[s2 length] + 1];
memccpy(string2, [s2 UTF8String], sizeof(char), [s2 length] + 1);
sort(&string1[0], &string1[0] + [s1 length]);
sort(&string2[0], &string2[0] + [s2 length]);
printf("string1 sorted = %s \n", string1);
printf("string2 sorted = %s \n", string2);
return strcmp(string1, string2) == 0;
}
|