LeetCode – Word Pattern (Java)

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.

Java Solution

public boolean wordPattern(String pattern, String str) {
    String[] arr = str.split(" ");  
    //prevent out of boundary problem
    if(arr.length != pattern.length())
        return false;
    HashMap<Character, String> map = new HashMap<Character, String>();
    for(int i=0; i<pattern.length(); i++){
        char c = pattern.charAt(i);
            String value = map.get(c);
                return false;
        }else if (map.containsValue(arr[i])){
            return false;
        map.put(c, arr[i]);
    return true;
Category >> Algorithms  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";