变位词
如果两个字符串的字符一样,出现的次数也一样,只是出现的顺序不一样,则认为两个字符串是兄弟字符串。比如,"abc"和"cba"就是兄弟字符串。
#include <stdio.h>
#include <string.h>
#define BOOL int
#define TRUE 1
#define FALSE 0
BOOL StrCheck(char * str1, char * str2){
char hash1[256] = {0};
char hash2[256] = {0};
int len1 = strlen(str1);
int len2 = strlen(str2);
if(len1 != len2){
return FALSE;
}
for(int i = 0;i<len1;i++){
hash1[str1[i]]++;
}
for(int i = 0;i<len2;i++){
hash2[str2[i]]++;
}
for(int i = 0;i<256;i++){
if(hash1[i] != hash2[i]){
return FALSE;
}
}
return TRUE;
}
int main(int argc, char * argv[]){
char * str[] = {"abd","bca","edf","aabc","cba"};
char * str2 = "abc";
for(int i = 0;i<sizeof(str)/sizeof(char *);i++){
if(StrCheck(str2,str[i])){
printf("%s\n",str[i]);
}
}
return 0;
}