# LeetCode – 290. Word Pattern

Given a `pattern` and a string `str`, find if `str` 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 `str`.

Examples:

1. pattern = `"abba"`, str = `"dog cat cat dog"` should return true.
2. pattern = `"abba"`, str = `"dog cat cat fish"` should return false.
3. pattern = `"aaaa"`, str = `"dog cat cat dog"` should return false.
4. pattern = `"abba"`, str = `"dog dog dog dog"` should return false.

Notes:
You may assume `pattern` contains only lowercase letters, and `str` contains lowercase letters separated by a single space.

```public class Solution {
public boolean wordPattern(String pattern, String str) {
// put all words in array
List sl = new ArrayList();
int pos = 0;
for(int i=0;i<str.length();i++){
if(str.charAt(i) == ' '){
pos = i+1;
}
}

//check if array size == pattern length
if(sl.size() != pattern.length()) return false;
HashMap<String,String> map = new HashMap();
for(int i=0;i<sl.size();i++){
String s = new String(String.valueOf(pattern.charAt(i)));
String l = new String(sl.get(i).toString());
if(!map.containsKey(s)){
if(map.containsValue(l)){
return false;
}
map.put(s,l);
}
else{
String e = new String(map.get(s));
if(!e.equals(l)){
return false;
}
}
}
return true;
}
}```

[0人评了分，平均: 0/5]