Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ValidWordAbbr {
- Set<String> set = new HashSet<>();
- Map<String, Integer> map = new HashMap<>();
- public ValidWordAbbr(String[] dictionary) {
- for(String w : dictionary)
- {
- if(set.contains(w)) continue;
- String str = shorten(w);
- set.add(w);
- map.put(str, map.getOrDefault(str, 0) + 1);
- }
- }
- String shorten(String str)
- {
- int n = str.length();
- if(n < 3) return str;
- return "" + str.charAt(0) + (n - 2) + str.charAt(n - 1);
- }
- public boolean isUnique(String word) {
- String str = shorten(word);
- if(set.contains(word) && map.get(str) < 2) return true;
- if(!set.contains(word) && !map.containsKey(str)) return true;
- return false;
- }
- }
- /**
- * Your ValidWordAbbr object will be instantiated and called as such:
- * ValidWordAbbr obj = new ValidWordAbbr(dictionary);
- * boolean param_1 = obj.isUnique(word);
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement