-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWord.java
More file actions
36 lines (34 loc) · 956 Bytes
/
Word.java
File metadata and controls
36 lines (34 loc) · 956 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
31
32
33
34
35
36
import java.util.HashMap;
public class Word {
public static void main(String[] args) {
String s, pattern;
wordPattern( pattern, s);
}
}
class Solution {
public boolean wordPattern(String pattern, String s) {
String[] word=s.split(" ");
if(pattern.length() != word.length){
return false;
}
HashMap<Character,String> map=new HashMap<>();
HashMap<String,Character> map2=new HashMap<>();
for(int i=0;i<pattern.length();i++){
char c=pattern.charAt(i);
String w=word[i];
if(map.containsKey(c)){
if(!map.get(c).equals(w)){
return false;
}
}
if(map2.containsKey(w)){
if(map2.get(w)!=c){
return false;
}
}
map.put(c, w);
map2.put(w, c);
}
return true;
}
}