|
28 | 28 | */
|
29 | 29 | public class _527 {
|
30 | 30 |
|
31 |
| - /**reference: https://discuss.leetcode.com/topic/82613/really-simple-and-straightforward-java-solution*/ |
32 |
| - public List<String> wordsAbbreviation(List<String> dict) { |
33 |
| - int len = dict.size(); |
34 |
| - String[] ans = new String[len]; |
35 |
| - int[] prefix = new int[len]; |
36 |
| - for (int i = 0; i < len; i++) { |
37 |
| - prefix[i] = 1; |
38 |
| - ans[i] = abbreviate(dict.get(i), 1); // make abbreviation for each string |
39 |
| - } |
40 |
| - for (int i = 0; i < len; i++) { |
41 |
| - while (true) { |
42 |
| - HashSet<Integer> set = new HashSet<>(); |
43 |
| - for (int j = i + 1; j < len; j++) { |
44 |
| - if (ans[j].equals(ans[i])) { |
45 |
| - set.add(j); // check all strings with the same abbreviation |
| 31 | + public static class Solution1 { |
| 32 | + /** |
| 33 | + * reference: https://discuss.leetcode.com/topic/82613/really-simple-and-straightforward-java-solution |
| 34 | + */ |
| 35 | + public List<String> wordsAbbreviation(List<String> dict) { |
| 36 | + int len = dict.size(); |
| 37 | + String[] ans = new String[len]; |
| 38 | + int[] prefix = new int[len]; |
| 39 | + for (int i = 0; i < len; i++) { |
| 40 | + prefix[i] = 1; |
| 41 | + ans[i] = abbreviate(dict.get(i), 1); // make abbreviation for each string |
| 42 | + } |
| 43 | + for (int i = 0; i < len; i++) { |
| 44 | + while (true) { |
| 45 | + HashSet<Integer> set = new HashSet<>(); |
| 46 | + for (int j = i + 1; j < len; j++) { |
| 47 | + if (ans[j].equals(ans[i])) { |
| 48 | + set.add(j); // check all strings with the same abbreviation |
| 49 | + } |
| 50 | + } |
| 51 | + if (set.isEmpty()) { |
| 52 | + break; |
| 53 | + } |
| 54 | + set.add(i); |
| 55 | + for (int k : set) { |
| 56 | + ans[k] = abbreviate(dict.get(k), ++prefix[k]); // increase the prefix |
46 | 57 | }
|
47 |
| - } |
48 |
| - if (set.isEmpty()) { |
49 |
| - break; |
50 |
| - } |
51 |
| - set.add(i); |
52 |
| - for (int k : set) { |
53 |
| - ans[k] = abbreviate(dict.get(k), ++prefix[k]); // increase the prefix |
54 | 58 | }
|
55 | 59 | }
|
| 60 | + return Arrays.asList(ans); |
56 | 61 | }
|
57 |
| - return Arrays.asList(ans); |
58 |
| - } |
59 | 62 |
|
60 |
| - private String abbreviate(String word, int k) { |
61 |
| - if (k + 2 >= word.length()) { |
62 |
| - return word; |
| 63 | + private String abbreviate(String word, int k) { |
| 64 | + if (k + 2 >= word.length()) { |
| 65 | + return word; |
| 66 | + } |
| 67 | + StringBuilder stringBuilder = new StringBuilder(); |
| 68 | + stringBuilder.append(word.substring(0, k)); |
| 69 | + stringBuilder.append(word.length() - 1 - k); |
| 70 | + stringBuilder.append(word.substring(word.length() - 1)); |
| 71 | + return stringBuilder.toString(); |
63 | 72 | }
|
64 |
| - StringBuilder stringBuilder = new StringBuilder(); |
65 |
| - stringBuilder.append(word.substring(0, k)); |
66 |
| - stringBuilder.append(word.length() - 1 - k); |
67 |
| - stringBuilder.append(word.substring(word.length() - 1)); |
68 |
| - return stringBuilder.toString(); |
69 | 73 | }
|
70 | 74 |
|
71 |
| - public static void main(String... args) { |
72 |
| - _527 test = new _527(); |
73 |
| - System.out.println(test.abbreviate("saaap", 2)); |
74 |
| - } |
75 | 75 | }
|
0 commit comments