easy way


  • 0
    H

    class Solution {
    public boolean wordPattern(String pattern, String str) {
    String[] strl = str.split(" ");
    char[] p = pattern.toCharArray();
    HashMap<String,String> match = new HashMap<>();
    if(strl.length!=p.length){
    return false;
    }

        for(int i=0;i<strl.length;i++){
            String sp = String.valueOf(p[i]);
            if(match.get(sp)==null){
                if(match.containsValue(strl[i])){
                    return false;
                }else{
                    match.put(sp,strl[i]);
                }
            }else{
                if(match.get(sp).equals(strl[i])){                    
                }else{
                    return false;
                }
            }            
        }
        return true;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.