--- title: 'LeetCode 242. Valid Anagram' disqus: hackmd --- # LeetCode 242. Valid Anagram ## Description 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. ## Example Input: s = "anagram", t = "nagaram" Output: true Input: s = "rat", t = "car" Output: false ## Constraints 1 <= s.length, t.length <= 5 * 10^4^ s and t consist of lowercase English letters. ## Answer 此題可用Hush table的概念,開26個空間,一個字母就是一空間,檢查s就++,檢查t就--,若完全符合則此空間檢查完之後仍全為0,若非即為false。 ```Cin= //0222_03_12 bool isAnagram(char * s, char * t){ int lens = strlen(s), lent = strlen(t); if(lens != lent){return false;} int *tab = (int*)malloc(sizeof(int)*26); for(int i = 0;i<26;i++){tab[i] = 0;} while(*s!='\0' && *t!='\0'){ tab[*s-'a']++; tab[*t-'a']--; s++; t++; } for(int i = 0;i<26;i++){if(tab[i] != 0){return false;}} return true; } ``` ## Link https://leetcode.com/problems/valid-anagram/ ###### tags: `Leetcode`