1.4

Write a method to decide if two strings are anagrams or not.

思路:使用一個count記錄每個字符出現的次數,若出現的次數都相同,說明兩個詞是變位詞

bool isStringAnagrams(char s[],char t[]){
     if(s==NULL||t==NULL||strlen(s)!=strlen(t))
         return false;
     int count[256];
     memset(count,0,sizeof(count));
     for(int i=0,len=strlen(s);i<len;i++){
         count[s[i]]++;
         count[t[i]]--;
     }
     for(int i=0;i<256;i++)
         if(count[i]!=0)
             return false;
     return true;
}

答案解法中的return i==len-1有些理解?????
發佈了286 篇原創文章 · 獲贊 8 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章