2ms Java Solution


  • 0
    X
    public class Solution {
    public boolean wordPattern(String pattern, String str) {
    	int l=pattern.length();
    	if (l < 1)
    	{
    		return false;
    	}
    	else
    	{
    		String [] strarray=str.split(" ");
    		int len=strarray.length;
    		if (len != pattern.length())
    		{
    			return false;
    		}
    		else
    		{
    			char [] pattern_array=pattern.toCharArray();
    			Map str_pattern=new HashMap();
    			Map pattern_pattern=new HashMap();
    			str_pattern.put(strarray[0],pattern_array[0]);
    			pattern_pattern.put(pattern_array[0], pattern_array[0]);
    			int i;
    			boolean flag=true;
    			for (i=1;i<len;i++)
    			{
    				if (str_pattern.put(strarray[i],pattern_array[i])!=pattern_pattern.put(pattern_array[i], pattern_array[i]))
    				{
    					flag=false;
    				}
    
    			}
    			return flag;
    		}
    	}
    }
    

    }


Log in to reply
 

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