java


  • 0
    M
    public boolean wordPattern(String pattern, String str) {
    	String[] temp=str.split(" ");
    	if(temp.length!=pattern.length()){
    		return false;
    	}
    	Map<String,String> findmap=new HashMap<String, String>();
    	for(int i=0;i<pattern.length();i++){
    		String keyp= String.valueOf(pattern.charAt(i));
    		if(findmap.containsKey(keyp)){
    			if(!findmap.get(keyp).equals(temp[i])){
    				return false;
    			}
    		}else{
    			if(findmap.containsValue(temp[i])){
    				return false;
    			}
    			findmap.put(keyp, temp[i]);
    		}
    	}
    	return true;
    }

Log in to reply
 

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