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);
        if(map.containsKey(c)){
            String value = map.get(c);
            if(!value.equals(arr[i])){
                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:
<pre><code> 
String foo = "bar";
</code></pre>

  1. Tarun Gulati on 2017-2-12

    Not a O(N) solution, to make it a O(N) linear solution we could use a reverse map as well.

  2. mg on 2017-4-30

    O(N) solution.. not splitting the str to words

    public boolean wordPattern(String pattern, String str) {

    int countOfWords = pattern.length();
    Map oneToOneMap = new HashMap();

    String word = null;
    int start = 0;
    char ch = ' ';

    for(int j = 0; j < str.length(); j++){
    if(countOfWords == 0){
    return false;
    }
    if(str.charAt(j) == ' '){
    //word
    word = str.substring(start, j);
    ch = pattern.charAt(pattern.length() - countOfWords);
    countOfWords--;
    start = j+1;
    if(!isValidRelation(oneToOneMap , ch, word)){
    return false;
    }
    }
    }

    if(start < str.length()){
    if(countOfWords == 0){
    return false;
    }
    word = str.substring(start);
    ch = pattern.charAt(pattern.length() - countOfWords);
    countOfWords--;
    if(!isValidRelation(oneToOneMap , ch, word)){
    return false;
    }
    }

    return countOfWords == 0;
    }

    boolean isValidRelation(Map oneToOneMap , char ch, String word){

    if(oneToOneMap.containsKey(ch)){
    if(!word.equals(oneToOneMap.get(ch))){
    return false;
    }
    }else if(oneToOneMap.containsValue(word)){
    return false;
    }else{
    oneToOneMap.put(ch, word);
    }

    return true;
    }

Leave a comment

*