-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordPattern.java
More file actions
30 lines (28 loc) · 1002 Bytes
/
WordPattern.java
File metadata and controls
30 lines (28 loc) · 1002 Bytes
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
package easy;
public class WordPattern {
public boolean wordPattern(String pattern, String str) {
String[] strs = str.split(" ");
if (pattern.length() != strs.length) return false;
for (int i = 1; i < pattern.length(); i++) {
boolean flag = true;
for (int j = i - 1; j >= 0; j--) {
if (pattern.charAt(i) == pattern.charAt(j)) {
if (!strs[i].equals(strs[j])) {
flag = false;
}
} else {
if (strs[i].equals(strs[j])) {
flag = false;
}
}
if (!flag) return false;
}
}
return true;
}
public static void main(String[] args) {
String pattern = "abba";
String str = "fish whoops helloworld fish";
System.out.println(new WordPattern().wordPattern(pattern, str));
}
}