-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode290.java
More file actions
35 lines (32 loc) · 945 Bytes
/
Copy pathleetcode290.java
File metadata and controls
35 lines (32 loc) · 945 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
package leetcode;
import java.util.HashMap;
public class leetcode290 {
/*
* leetcode 205 同解法
* */
public boolean wordPattern(String pattern, String str) {
String patternToNum=StringTonums(pattern,"");
String strTonum=StringTonums(str," ");
return strTonum.equals(patternToNum);
}
public String StringTonums(String str,String re) {
String [] strings=str.split(re);
StringBuilder res=new StringBuilder();
int num=1;
HashMap<String,Integer> map=new HashMap<>();
for (String s:
strings) {
if (!map.containsKey(s)){
map.put(s,num);
num++;
}
}
for (String s:strings){
res.append(map.get(s));
}
return res.toString();
}
public static void main(String[] args) {
new leetcode290().wordPattern("abba","dog cat cat dog");
}
}