Skip to content

Latest commit

 

History

History
74 lines (56 loc) · 1.54 KB

_242. Valid Anagram.md

File metadata and controls

74 lines (56 loc) · 1.54 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 13, 2024

Last updated : July 01, 2024


Related Topics : Hash Table, String, Sorting

Acceptance Rate : 65.77 %


Solutions

Python

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        cntS, cntT = Counter(s), Counter(t)

        if not cntS == cntT :
            return False

        for key in cntS :
            if cntS.get(key) != cntT.get(key) :
                return False

        return True
class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        return (len(s) == len(t)) and (Counter(s) == Counter(t))

C

bool isAnagram(char* s, char* t) {
    int letters[26] = {0};

    while (*s) {
        letters[*s - 'a']++;
        s++;
    }

    while (*t) {
        letters[*t - 'a']--;
        t++;
    }

    for (int i = 0; i < 26; i++) {
        if (letters[i]) {
            return false;
        }
    }
    return true;
}