# [LeetCode 125. Valid Palindrome] ###### tags: `Leetcode` * 題目 Given two strings s and t, return true if t is an anagram of s, and false otherwise. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once * 解法1 * 先判斷char長度是否相同(經測試,strlen這個很耗效能) * 因為知道這題給的字元是只有a-z,所以就直接建立一個26個位置的陣列c,然後先從第一組的char找到陣列c對應的位置加一(ex:'a'就把c[0]++),再來第二組則相反(ex:'b'就把c[1]--),最後在判斷c是否每個都是0,不是就return false反之return true ``` bool isAnagram(char * s, char * t){ if(strlen(s) != strlen(t)) return false; int c[26] = {0}; //宣告26個0的陣列 for(int i =0; i < strlen(s); i++) { c[s[i]-'a']++; c[t[i]-'a']--; } for(int i =0; i < 26; i++) if(c[i]!=0) return false; return true; } ``` * 解法2 * 把解法1的第二點分成兩個迴圈,即可先不用判斷長度 ``` bool isAnagram(char * s, char * t){ int c[26] = {0}; //宣告26個0的陣列 int i = 0; while (s[i] != '\0') { c[s[i]-'a']++; i++; } i=0; while (t[i] != '\0') { c[t[i]-'a']--; i++; } for(int i =0; i < 26; i++) if(c[i]!=0) return false; return true; } ```