字符串是匹配

来源:互联网 发布:北京握奇数据系统待遇 编辑:程序博客网 时间:2024/05/22 05:10
假设两个字符串中所含有的字符和个数都相同我们就叫这两个字符串匹配,
 比如:abcda和adabc,由于出现的字符个数都是相同,只是顺序不同,
 所以这两个字符串是匹配的。要求高效!

int Match_String(char *s1,char *s2){int temp1=strlen(s1);int hash[26]={0}; //测试使用的全部是小写字母,所以在这里只设定了26个空间for (int i=0;i<temp1;i++){int index=s1[i]-'a';hash[index]++;}for (i=0;i<temp1;i++){int index=s2[i]-'a';hash[index]--;}for (i=0;i<26;i++){if (hash[i]!=0){return -1; //不匹配}}return 0;}