Given a pattern and a string s, find if s&nbsp;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.

&nbsp;
Example 1:

Input: pattern = "abba", s = "dog cat cat dog"
Output: true


Example 2:

Input: pattern = "abba", s = "dog cat cat fish"
Output: false


Example 3:

Input: pattern = "aaaa", s = "dog cat cat dog"
Output: false


&nbsp;
Constraints:


	1 &lt;= pattern.length &lt;= 300
	pattern contains only lower-case English letters.
	1 &lt;= s.length &lt;= 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.


class Solution(object):
    def wordPattern(self, pattern, s):
        """
        :type pattern: str
        :type s: str
        :rtype: bool
        """
        p, s = list(pattern), list(s.split(" "))

        return len(s) == len(p) and len(set(zip(p, s))) == len(set(s)) == len(set(p))
class Solution {
    public boolean wordPattern(String pattern, String s) {
        String[] arr=s.split(" ");
         if(pattern.length()!=arr.length) return false;
        Map<Character,String> map=new HashMap<Character,String>();
        
        for(int i=0;i<pattern.length();i++){
            char ch=pattern.charAt(i);
            if(map.containsKey(ch)){
                 if(!map.get(ch).equals(arr[i])){
                    return false;
                }    
            }else if(!(map.containsKey(ch)|| map.containsValue(arr[i]))){
               map.put(ch,arr[i]);
            }else{
                return false;
            }
        }
        return true;
    }
}
class Solution {
public:

    unordered_set<string> processed_words;
    string m[26]; // char to string mapping

    bool crunch_next_word(char c, string word)
    {
        int idx = c-'a';
        if(m[idx].empty() && processed_words.count(word)==0)
        {
            m[idx] = word;
            processed_words.insert(word);
            return true;
        }
        else if(m[idx]==word) return true;
        else return false;
    }

    bool wordPattern(string pattern, string s)
    {
        int count = 0;

        int start = 0;
        int end = s.find(' ');
        while (end != -1)
        {
            string word = s.substr(start, end - start);
            char c = pattern[count];
            if(!crunch_next_word(c,word)) return false;

            start = end + 1;
            end = s.find(' ', start);
            count++;
            if(count == pattern.length()) return false;
        }
        if(count != pattern.length()-1) return false;
        string word = s.substr(start, end - start);
        char c = pattern[count];
        if(!crunch_next_word(c,word)) return false;
        return true;
    }
};
var wordPattern = function(pattern, s) {
    let wordArray = s.split(" ");
    if(wordArray.length !== pattern.length) return false;

    let hm = {};
    let hs = new Set();

    for(let index in pattern) {
        let word = wordArray[index];
        let char = pattern[index];

        if(hm[char] !== undefined) {
            if(hm[char] !== word) return false;
        } else {
            if(hs.has(word)) return false; // Duplicate Occurence of word on first occurrence of a char
            hm[char] = word;
            hs.add(word);
        }
    }

    return true;
}
Word Pattern