bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 290. Word Pattern

290. Word Pattern

Leetcode Solutions

Approach: Two Hash Maps

  1. Split s into words based on spaces.
  2. If the number of words does not match the length of pattern, return False.
  3. Initialize two empty hash maps: char_to_word and word_to_char.
  4. Iterate over the characters in pattern and the corresponding words in s. a. If the character is not in char_to_word, check if the word is in word_to_char. If it is, return False. b. If the character is in char_to_word, check if it maps to a different word. If it does, return False. c. Update char_to_word and word_to_char with the new character-word pair.
  5. If all character-word pairs are processed without conflicts, return True.
UML Thumbnail

Approach: Single Index Hash Map

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...