Skip to content

Commit 0ba2286

Browse files
refactor 527
1 parent 5cebad4 commit 0ba2286

File tree

2 files changed

+60
-36
lines changed

2 files changed

+60
-36
lines changed

src/main/java/com/fishercoder/solutions/_527.java

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,48 +28,48 @@
2828
*/
2929
public class _527 {
3030

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
4657
}
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
5458
}
5559
}
60+
return Arrays.asList(ans);
5661
}
57-
return Arrays.asList(ans);
58-
}
5962

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();
6372
}
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();
6973
}
7074

71-
public static void main(String... args) {
72-
_527 test = new _527();
73-
System.out.println(test.abbreviate("saaap", 2));
74-
}
7575
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._527;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import java.util.Arrays;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class _527Test {
12+
private static _527.Solution1 solution1;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _527.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
assertEquals(Arrays.asList("l2e", "god", "internal", "me", "i6t", "interval", "inte4n", "f2e", "intr4n"), solution1.wordsAbbreviation(Arrays.asList("like", "god", "internal", "me", "internet", "interval", "intension", "face", "intrusion")));
22+
}
23+
24+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy