We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2d39e56 commit 038b67fCopy full SHA for 038b67f
WordPattern.java
@@ -0,0 +1,26 @@
1
+import java.util.HashMap;
2
+
3
+// my solution_O(n)
4
+public class WordPattern {
5
+ public boolean wordPattern(String pattern, String s) {
6
+ HashMap<Character, String> hashMap = new HashMap<>();
7
+ StringBuffer strPattern = new StringBuffer(pattern);
8
+ String[] str = s.split(" ");
9
10
+ if(str.length != strPattern.length())
11
+ return false;
12
13
+ for(int i = 0; i<str.length; i++){
14
+ if(!hashMap.containsKey(strPattern.charAt(i))) {
15
+ if(hashMap.containsValue(str[i]))
16
17
+ hashMap.put(strPattern.charAt(i), str[i]);
18
+ }
19
+ else {
20
+ if(!hashMap.get(strPattern.charAt(i)).equals(str[i]))
21
22
23
24
+ return true;
25
26
+}
0 commit comments