My java solution


  • 0
    L
    public class Solution {
    public boolean wordPattern(String pattern, String str) {
        String[] strs=str.split(" ");
        if(pattern.length()!=strs.length) return false;
        Map<Character,String> map=new HashMap<Character,String>();
        for(int i=0;i<strs.length;i++){
            if(map.containsKey(pattern.charAt(i))){
                String value=map.get(pattern.charAt(i));
                if(value.equals(strs[i])==false) return false;
            }
            else if(map.containsValue(strs[i])){
                return false;
            }
            else{
                map.put(pattern.charAt(i),strs[i]);
            }
        }
        return true;
    }
    

    }


Log in to reply
 

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