Created
October 13, 2016 22:45
-
-
Save Roger7410/acd922bad85cd5ea35d50393f7a8063d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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