Tensorum
YouTubeనేర్చుకోవడం ప్రారంభించండి
LeetCode 290 · Hashing / Frequency

Word Pattern

LeetCode 290 · సులభం · పరిష్కారం, intuition & దశలవారీ వాక్‌త్రూ

Last-seen index per letter and per word must agree.

సులభంHash TableString
LeetCode లో తెరవండి ↗

నేను Word Pattern (LeetCode 290) ను ఇలా పరిష్కరించాను — Hashing / Frequency ప్యాటర్న్‌లోని ఒక సులభం ప్రాబ్లమ్. కింద: intuition, దశలవారీ approach, నా accepted Python కోడ్, animated test cases, line-by-line debugger.

కింది వివరణ ఇంగ్లీష్‌లో ఉంది.

Intuition

Same idea as isomorphic strings, but between pattern letters and words: each letter and its word must have been last seen at the same index.

Approach

  1. 1

    Split s into words; lengths must match.

  2. 2

    Compare stored indices for pattern[i] and words[i].

  3. 3

    Record the current index for both.

నేను ఎలా పరిష్కరించాను

Last-seen indicesPythonO(n) time · O(n) space
class Solution:
    def wordPattern(self, pattern: str, s: str) -> bool:
        pattern_mpp = {}
        s_mpp = {}
        words = s.split(" ")
        if len(pattern)!=len(words):
            return False
        for i in range(len(pattern)):
            char = pattern[i]
            word = words[i]
            if pattern_mpp.get(char,-1)!=s_mpp.get(word,-1):
                return False
            pattern_mpp[char]=i
            s_mpp[word]=i
        return True
స్టెప్-త్రూ లోడ్ అవుతోంది…

Word Pattern — తరచూ అడిగే ప్రశ్నలు

Word Pattern వెనుక ఉన్న intuition ఏమిటి?

Same idea as isomorphic strings, but between pattern letters and words: each letter and its word must have been last seen at the same index.

Word Pattern (LeetCode 290) ను ఎలా పరిష్కరించాలి?

1. Split s into words; lengths must match. 2. Compare stored indices for pattern[i] and words[i]. 3. Record the current index for both.

Word Pattern యొక్క time, space complexity ఎంత?

Time: O(n). Space: O(n). (Accepted approach: Last-seen indices.)

Word Pattern లో సాధారణ పొరపాట్లు ఏమిటి?

Length mismatch is an immediate False.