Simple java solution.

  • 0

    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.containsValue(s[i])) return false;
    			hm.put(ptrn[i], s[i]);
    			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.