-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWordPattern.java
More file actions
38 lines (30 loc) · 1.35 KB
/
Copy pathWordPattern.java
File metadata and controls
38 lines (30 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Given a pattern and a string str, find if str follows the same pattern.
// Here follow means a full match, such that there is a bijection between a letter in pattern
// and a non-empty word in str.
// See: https://leetcode.com/problems/word-pattern/
package leetcode.array_and_hashtable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
public class WordPattern {
public boolean wordPattern(String pattern, String str) {
String[] words = str.split(" ");
if (words.length != pattern.length()) return false;
char[] chars = pattern.toCharArray();
Map<Character, String> map = new HashMap<>();
for (int i = 0; i < chars.length; i++) {
if (map.containsKey(chars[i]) && !map.get(chars[i]).equals(words[i]))
return false;
else map.put(chars[i], words[i]);
}
return map.size() == new HashSet<>(Arrays.asList(words)).size();
}
public static void main(String[] args) {
WordPattern sln = new WordPattern();
System.out.println(sln.wordPattern("abba", "dog cat cat dog"));
System.out.println(sln.wordPattern("abba", "dog cat cat fish"));
System.out.println(sln.wordPattern("aaaa", "dog cat cat dog"));
System.out.println(sln.wordPattern("abba", "dog dog dog dog"));
}
}