290. Word Pattern Leetcode Solution

Word Pattern Leetcode Problem :

Given a pattern and a string s, find if s follows the same pattern.

Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in s.

jump game leetcode

Word Pattern Leetcode Solution :

Constraints :

  • 1 <= pattern.length <= 300
  • pattern contains only lower-case English letters.
  • 1 <= s.length <= 3000
  • s contains only lowercase English letters and spaces ‘ ‘.
  • s does not contain any leading or trailing spaces.
  • All the words in s are separated by a single space.

Example 1:

  • Input: pattern = “abba”, s = “dog cat cat fish”
  • Output: false

Example 2:

  • Input: pattern = “aaaa”, s = “dog cat cat dog”
  • Output: false

Observation :

-Length should be same, if not then its not possible
-we have to map the character with the the string
-if any other value for this key, we have to return false

Approach :

-To map the Character with the String we have to use the Map
-Put the Character with String if not present
-if Its present then see, whether this key value are same or not, if not same then return the false but,
-if the value that we are adding is already had seen or use or other key value, then return false, hence we use the hashset of strings


Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Code :

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription