Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4c6cc66

Browse files
add 527
1 parente320d72 commit4c6cc66

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+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.Arrays;
4+
importjava.util.HashSet;
5+
importjava.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+
publicclass_527 {
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]))set.add(j);// check all strings with the same abbreviation
45+
}
46+
if (set.isEmpty())break;
47+
set.add(i);
48+
for (intk :set)
49+
ans[k] =abbreviate(dict.get(k), ++prefix[k]);// increase the prefix
50+
}
51+
}
52+
returnArrays.asList(ans);
53+
}
54+
55+
privateStringabbreviate(Stringword,intk) {
56+
if (k +2 >=word.length()) {
57+
returnword;
58+
}
59+
StringBuilderstringBuilder =newStringBuilder();
60+
stringBuilder.append(word.substring(0,k));
61+
stringBuilder.append(word.length() -1 -k);
62+
stringBuilder.append(word.substring(word.length()-1));
63+
returnstringBuilder.toString();
64+
}
65+
66+
publicstaticvoidmain(String...args) {
67+
_527test =new_527();
68+
System.out.println(test.abbreviate("saaap",2));
69+
}
70+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp