Skip to content

Commit 4c6cc66

Browse files
add 527
1 parent e320d72 commit 4c6cc66

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ Your ideas/fixes/algorithms are more than welcome!
100100
|531|[Lonely Pixel I](https://leetcode.com/problems/lonely-pixel-i/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_531.java) | O(m*n) |O(1) | Medium |
101101
|530|[Minimum Absolute Difference in BST](https://leetcode.com/problems/minimum-absolute-difference-in-bst/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_530.java) | O(n) |O(n) | Easy| DFS
102102
|529|[Minesweeper](https://leetcode.com/problems/minesweeper/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_529.java) | O(m*n) |O(k) | Medium | BFS
103+
|527|[Word Abbreviation](https://leetcode.com/problems/word-abbreviation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_527.java) | O(n^2) |O(n) | Hard |
103104
|526|[Beautiful Arrangement](https://leetcode.com/problems/beautiful-arrangement/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_526.java) | O(n) |O(h) | Medium | Backtracking
104105
|525|[Contiguous Array](https://leetcode.com/problems/contiguous-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_525.java) | O(n) |O(n) | Medium | HashMap
105106
|524|[Longest Word in Dictionary through Deleting](https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_524.java) | O(n) |O(n) | Medium | Sort
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
import java.util.List;
6+
7+
/**
8+
* 527. Word Abbreviation
9+
*
10+
* Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations for every word following rules below.
11+
12+
Begin with the first character and then the number of characters abbreviated, which followed by the last character.
13+
If there are any conflict, that is more than one words share the same abbreviation,
14+
a longer prefix is used instead of only the first character until making the map
15+
from word to abbreviation become unique.
16+
In other words, a final abbreviation cannot map to more than one original words.
17+
If the abbreviation doesn't make the word shorter, then keep it as original.
18+
19+
Example:
20+
Input: ["like", "god", "internal", "me", "internet", "interval", "intension", "face", "intrusion"]
21+
Output: ["l2e","god","internal","me","i6t","interval","inte4n","f2e","intr4n"]
22+
23+
Note:
24+
Both n and the length of each word will not exceed 400.
25+
The length of each word is greater than 1.
26+
The words consist of lowercase English letters only.
27+
The return answers should be in the same order as the original array.
28+
*/
29+
public class _527 {
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])) set.add(j); // check all strings with the same abbreviation
45+
}
46+
if (set.isEmpty()) break;
47+
set.add(i);
48+
for (int k : set)
49+
ans[k] = abbreviate(dict.get(k), ++prefix[k]); // increase the prefix
50+
}
51+
}
52+
return Arrays.asList(ans);
53+
}
54+
55+
private String abbreviate(String word, int k) {
56+
if (k + 2 >= word.length()) {
57+
return word;
58+
}
59+
StringBuilder stringBuilder = new StringBuilder();
60+
stringBuilder.append(word.substring(0, k));
61+
stringBuilder.append(word.length() - 1 - k);
62+
stringBuilder.append(word.substring(word.length()-1));
63+
return stringBuilder.toString();
64+
}
65+
66+
public static void main(String... args) {
67+
_527 test = new _527();
68+
System.out.println(test.abbreviate("saaap", 2));
69+
}
70+
}

0 commit comments

Comments
 (0)