Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 242 · Hashing / Anagram

Valid Anagram

LeetCode 242 · எளிது · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

One frequency table: +1 for s, −1 for t, all zeros at the end.

எளிதுHash TableStringSorting
LeetCode இல் திற ↗

நான் Valid Anagram (LeetCode 242) ஐ இப்படித் தீர்த்தேன் — Hashing / Anagram பேட்டர்னின் ஒரு எளிது பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.

Intuition

Two strings are anagrams when every letter appears the same number of times in both. Count letters of s up and letters of t down in one 26-slot table; if every slot ends at zero, the counts match.

Approach

  1. 1

    Different lengths can't be anagrams.

  2. 2

    Map each letter to an index 0–25 with ord(c) − ord('a').

  3. 3

    Increment for s[i], decrement for t[i] in the same loop.

  4. 4

    Return True if every count is zero.

நான் எப்படித் தீர்த்தேன்

Frequency tablePythonO(n) time · O(1) space
class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        if len(s)!=len(t):
            return False
        n = len(s)
        freq_table = [0]*26
        for i in range(n):
            char1 = s[i]
            char2 = t[i]
            """
            ord(a) = 97
            ord(b) = 98
            ord('a') - ord('a') =0
            ord('b') - ord('a') = 1
            ord('c') - ord('a') = 2
            """
            freq_table[ord(char1)-ord('a')]+=1
            freq_table[ord(char2)-ord('a')]-=1
        return all(x==0 for x in freq_table)
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Valid Anagram — அடிக்கடி கேட்கப்படும் கேள்விகள்

Valid Anagram பின்னாலுள்ள intuition என்ன?

Two strings are anagrams when every letter appears the same number of times in both. Count letters of s up and letters of t down in one 26-slot table; if every slot ends at zero, the counts match.

Valid Anagram (LeetCode 242) ஐ எப்படித் தீர்ப்பது?

1. Different lengths can't be anagrams. 2. Map each letter to an index 0–25 with ord(c) − ord('a'). 3. Increment for s[i], decrement for t[i] in the same loop. 4. Return True if every count is zero.

Valid Anagram இன் time மற்றும் space complexity என்ன?

Time: O(n). Space: O(1) — 26 counters. (Accepted approach: Frequency table.)

Valid Anagram இல் பொதுவான தவறுகள் என்ன?

Unicode input would need a dict instead of a fixed 26-slot array.