Skip to content

Commit 1a0c104

Browse files
style: format TopKFrequentWords files with clang-format
1 parent 843d463 commit 1a0c104

File tree

2 files changed

+7
-34
lines changed

2 files changed

+7
-34
lines changed

src/main/java/com/thealgorithms/strings/TopKFrequentWords.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
* <p>Words are ranked by frequency in descending order. For equal frequencies,
1313
* words are ranked in lexicographical ascending order.
1414
*
15+
* <p>Reference:
16+
* https://en.wikipedia.org/wiki/Top-k_problem
17+
*
1518
*/
1619
public final class TopKFrequentWords {
1720
private TopKFrequentWords() {
@@ -45,11 +48,7 @@ public static List<String> findTopKFrequentWords(String[] words, int k) {
4548
}
4649

4750
List<String> candidates = new ArrayList<>(frequency.keySet());
48-
candidates.sort(
49-
Comparator.<String>comparingInt(frequency::get)
50-
.reversed()
51-
.thenComparing(Comparator.naturalOrder())
52-
);
51+
candidates.sort(Comparator.<String>comparingInt(frequency::get).reversed().thenComparing(Comparator.naturalOrder()));
5352

5453
int limit = Math.min(k, candidates.size());
5554
return new ArrayList<>(candidates.subList(0, limit));

src/test/java/com/thealgorithms/strings/TopKFrequentWordsTest.java

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,8 @@ void testFindTopKFrequentWords(String[] words, int k, List<String> expected) {
1818
}
1919

2020
static Stream<Arguments> validTestCases() {
21-
return Stream.of(
22-
Arguments.of(
23-
new String[] {"i", "love", "leetcode", "i", "love", "coding"},
24-
2,
25-
List.of("i", "love")
26-
),
27-
Arguments.of(
28-
new String[] {"the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"},
29-
4,
30-
List.of("the", "is", "sunny", "day")
31-
),
32-
Arguments.of(
33-
new String[] {"bbb", "aaa", "bbb", "aaa", "ccc"},
34-
2,
35-
List.of("aaa", "bbb")
36-
),
37-
Arguments.of(
38-
new String[] {"one", "two", "three"},
39-
10,
40-
List.of("one", "three", "two")
41-
),
42-
Arguments.of(new String[] {}, 3, List.of()),
43-
Arguments.of(new String[] {"x", "x", "y"}, 0, List.of())
44-
);
21+
return Stream.of(Arguments.of(new String[] {"i", "love", "leetcode", "i", "love", "coding"}, 2, List.of("i", "love")), Arguments.of(new String[] {"the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"}, 4, List.of("the", "is", "sunny", "day")),
22+
Arguments.of(new String[] {"bbb", "aaa", "bbb", "aaa", "ccc"}, 2, List.of("aaa", "bbb")), Arguments.of(new String[] {"one", "two", "three"}, 10, List.of("one", "three", "two")), Arguments.of(new String[] {}, 3, List.of()), Arguments.of(new String[] {"x", "x", "y"}, 0, List.of()));
4523
}
4624

4725
@ParameterizedTest
@@ -51,10 +29,6 @@ void testFindTopKFrequentWordsInvalidInput(String[] words, int k) {
5129
}
5230

5331
static Stream<Arguments> invalidTestCases() {
54-
return Stream.of(
55-
Arguments.of((String[]) null, 1),
56-
Arguments.of(new String[] {"a", null, "b"}, 2),
57-
Arguments.of(new String[] {"a"}, -1)
58-
);
32+
return Stream.of(Arguments.of((String[]) null, 1), Arguments.of(new String[] {"a", null, "b"}, 2), Arguments.of(new String[] {"a"}, -1));
5933
}
6034
}

0 commit comments

Comments
 (0)