Note:
This is kind of recurrsive dedup questions.
class Solution { public ListwordsAbbreviation(List dict) { List result = new ArrayList<>(); int[] prefix = new int[dict.size()]; String[] current = new String[dict.size()]; for (int i = 0; i < current.length; i++) { prefix[i] = 1; current[i] = addAbb(dict.get(i), 1); } for (int i = 0; i < current.length; i++) { while (true) { Set dups = new HashSet<>(); for (int j = i + 1; j < current.length; j++) { if (current[j].equals(current[i])) { dups.add(j); } } if (dups.size() == 0) break; dups.add(i); for (int dup : dups) { current[dup] = addAbb(dict.get(dup), ++prefix[dup]); } } result.add(current[i]); } return result; } private String addAbb(String s, int len) { if (len >= s.length() - 2) return s; StringBuilder result = new StringBuilder(); result.append(s.substring(0, len)).append(s.length() - len - 1).append(s.charAt(s.length() - 1)); return result.toString(); }}