|
28 | 28 | */
|
29 | 29 | publicclass_527 {
|
30 | 30 |
|
31 |
| -/**reference: https://discuss.leetcode.com/topic/82613/really-simple-and-straightforward-java-solution*/ |
32 |
| -publicList<String>wordsAbbreviation(List<String>dict) { |
33 |
| -intlen =dict.size(); |
34 |
| -String[]ans =newString[len]; |
35 |
| -int[]prefix =newint[len]; |
36 |
| -for (inti =0;i <len;i++) { |
37 |
| -prefix[i] =1; |
38 |
| -ans[i] =abbreviate(dict.get(i),1);// make abbreviation for each string |
39 |
| - } |
40 |
| -for (inti =0;i <len;i++) { |
41 |
| -while (true) { |
42 |
| -HashSet<Integer>set =newHashSet<>(); |
43 |
| -for (intj =i +1;j <len;j++) { |
44 |
| -if (ans[j].equals(ans[i])) { |
45 |
| -set.add(j);// check all strings with the same abbreviation |
| 31 | +publicstaticclassSolution1 { |
| 32 | +/** |
| 33 | + * reference: https://discuss.leetcode.com/topic/82613/really-simple-and-straightforward-java-solution |
| 34 | + */ |
| 35 | +publicList<String>wordsAbbreviation(List<String>dict) { |
| 36 | +intlen =dict.size(); |
| 37 | +String[]ans =newString[len]; |
| 38 | +int[]prefix =newint[len]; |
| 39 | +for (inti =0;i <len;i++) { |
| 40 | +prefix[i] =1; |
| 41 | +ans[i] =abbreviate(dict.get(i),1);// make abbreviation for each string |
| 42 | + } |
| 43 | +for (inti =0;i <len;i++) { |
| 44 | +while (true) { |
| 45 | +HashSet<Integer>set =newHashSet<>(); |
| 46 | +for (intj =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 (intk :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 (intk :set) { |
53 |
| -ans[k] =abbreviate(dict.get(k), ++prefix[k]);// increase the prefix |
54 | 58 | }
|
55 | 59 | }
|
| 60 | +returnArrays.asList(ans); |
56 | 61 | }
|
57 |
| -returnArrays.asList(ans); |
58 |
| - } |
59 | 62 |
|
60 |
| -privateStringabbreviate(Stringword,intk) { |
61 |
| -if (k +2 >=word.length()) { |
62 |
| -returnword; |
| 63 | +privateStringabbreviate(Stringword,intk) { |
| 64 | +if (k +2 >=word.length()) { |
| 65 | +returnword; |
| 66 | + } |
| 67 | +StringBuilderstringBuilder =newStringBuilder(); |
| 68 | +stringBuilder.append(word.substring(0,k)); |
| 69 | +stringBuilder.append(word.length() -1 -k); |
| 70 | +stringBuilder.append(word.substring(word.length() -1)); |
| 71 | +returnstringBuilder.toString(); |
63 | 72 | }
|
64 |
| -StringBuilderstringBuilder =newStringBuilder(); |
65 |
| -stringBuilder.append(word.substring(0,k)); |
66 |
| -stringBuilder.append(word.length() -1 -k); |
67 |
| -stringBuilder.append(word.substring(word.length() -1)); |
68 |
| -returnstringBuilder.toString(); |
69 | 73 | }
|
70 | 74 |
|
71 |
| -publicstaticvoidmain(String...args) { |
72 |
| -_527test =new_527(); |
73 |
| -System.out.println(test.abbreviate("saaap",2)); |
74 |
| - } |
75 | 75 | }
|