My java solution


  • 0
    C
           import java.util.*;
           public class Solution {
           public boolean wordPattern(String pattern, String str) {
         
    	String[] split = str.split(" ");
    	
    	if(split.length != pattern.length())return false;
    	
    	Hashtable<Character,String> table = new Hashtable();
    	
    	for(int i=0;i<pattern.length();i++)
    	{
    		String value = table.get(pattern.charAt(i));
    		if(value == null && !(table.containsValue(split[i])))
    			table.put(pattern.charAt(i), split[i]);
    		
    		else if(value == null && table.containsValue(split[i]))
    			return false;
    		
    		else if(!value.equals(split[i]))
    			return false;
    	}
    	
    	return true;
    }
    

    }


Log in to reply
 

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