-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathValidWordAbbr.java
More file actions
37 lines (35 loc) · 1.21 KB
/
Copy pathValidWordAbbr.java
File metadata and controls
37 lines (35 loc) · 1.21 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
package leetcode;
import java.util.*;
public class ValidWordAbbr {
HashMap<String,String> set = new HashMap<String,String>();
public ValidWordAbbr(String[] dictionary) {
for(String s:dictionary){
String convert = convert(s);
if(set.containsKey(convert) && !set.get(convert).equals(s))
set.put(convert,"");
else
set.put(convert,s);
}
}
private String convert(String s){
if(s.length() == 0) return "";
char[] arr = {s.charAt(0),(char)('0'+Math.max(0, s.length()-2)),s.charAt(s.length()-1)};
return new String(arr);
}
public boolean isUnique(String word) {
String convert = this.convert(word);
if( !set.containsKey(convert) || set.get(convert).equals(word))
return true;
return false;
}
public static void main(String[] args) {
String[] dictionary = {"deer", "door", "cake", "card" };
ValidWordAbbr vw = new ValidWordAbbr(dictionary);
System.out.println(vw.set);
System.out.println(vw.isUnique("dear"));
System.out.println(vw.isUnique("cart"));
System.out.println(vw.isUnique("cane"));
System.out.println(vw.isUnique("make"));
System.out.println(vw.isUnique("door"));
}
}