Skip to content

Instantly share code, notes, and snippets.

@Roger7410
Created October 13, 2016 22:45
Show Gist options
  • Select an option

  • Save Roger7410/acd922bad85cd5ea35d50393f7a8063d to your computer and use it in GitHub Desktop.

Select an option

Save Roger7410/acd922bad85cd5ea35d50393f7a8063d to your computer and use it in GitHub Desktop.
public class Solution {
public boolean wordPattern(String pattern, String str) {
String[] arr= str.split(" ");
HashMap<Character, String> map = new HashMap<Character, String>();
if(arr.length!= pattern.length())
return false;
for(int i=0; i<arr.length; i++){
char c = pattern.charAt(i);
if(map.containsKey(c)){
if(!map.get(c).equals(arr[i]))
return false;
}else{
if(map.containsValue(arr[i]))
return false;
map.put(c, arr[i]);
}
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment