Simple java solution.


  • 0
    S

    char[] ptrn=pattern.toCharArray();
    String[] s=str.split(" ");
    if(ptrn.length!=s.length)return false;

    	HashMap<Character,String> hm=new HashMap<>();
    	for(int i=0;i<ptrn.length;i++){
    		if(!hm.containsKey(ptrn[i])){
    			if(hm.containsValue(s[i])) return false;
    			hm.put(ptrn[i], s[i]);
    		}else{
    			String prevStr=hm.get(ptrn[i]);
    			String curStr=s[i];
    			if(!prevStr.equals(curStr))return false;
    		}
    	}
    	return true;

Log in to reply
 

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