Ai
1 Star 0 Fork 0

徐长贺/Leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
_527.java 2.88 KB
一键复制 编辑 原始数据 按行查看 历史
Fisher Coder 提交于 2019-06-13 00:46 +08:00 . refactor 527
package com.fishercoder.solutions;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
/**
* 527. Word Abbreviation
*
* Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations for every word following rules below.
Begin with the first character and then the number of characters abbreviated, which followed by the last character.
If there are any conflict, that is more than one words share the same abbreviation,
a longer prefix is used instead of only the first character until making the map
from word to abbreviation become unique.
In other words, a final abbreviation cannot map to more than one original words.
If the abbreviation doesn't make the word shorter, then keep it as original.
Example:
Input: ["like", "god", "internal", "me", "internet", "interval", "intension", "face", "intrusion"]
Output: ["l2e","god","internal","me","i6t","interval","inte4n","f2e","intr4n"]
Note:
Both n and the length of each word will not exceed 400.
The length of each word is greater than 1.
The words consist of lowercase English letters only.
The return answers should be in the same order as the original array.
*/
public class _527 {
public static class Solution1 {
/**
* reference: https://discuss.leetcode.com/topic/82613/really-simple-and-straightforward-java-solution
*/
public List<String> wordsAbbreviation(List<String> dict) {
int len = dict.size();
String[] ans = new String[len];
int[] prefix = new int[len];
for (int i = 0; i < len; i++) {
prefix[i] = 1;
ans[i] = abbreviate(dict.get(i), 1); // make abbreviation for each string
}
for (int i = 0; i < len; i++) {
while (true) {
HashSet<Integer> set = new HashSet<>();
for (int j = i + 1; j < len; j++) {
if (ans[j].equals(ans[i])) {
set.add(j); // check all strings with the same abbreviation
}
}
if (set.isEmpty()) {
break;
}
set.add(i);
for (int k : set) {
ans[k] = abbreviate(dict.get(k), ++prefix[k]); // increase the prefix
}
}
}
return Arrays.asList(ans);
}
private String abbreviate(String word, int k) {
if (k + 2 >= word.length()) {
return word;
}
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(word.substring(0, k));
stringBuilder.append(word.length() - 1 - k);
stringBuilder.append(word.substring(word.length() - 1));
return stringBuilder.toString();
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/isulong/Leetcode.git
git@gitee.com:isulong/Leetcode.git
isulong
Leetcode
Leetcode
master

搜索帮助