-
Notifications
You must be signed in to change notification settings - Fork 12
/
solution.java
34 lines (29 loc) · 1.11 KB
/
solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.*;
class Solution {
public String kthDistinct(String[] arr, int k) {
// Create a HashMap to count occurrences of each string
Map<String, Integer> count = new HashMap<>();
// Create a List to store distinct strings
List<String> distinct = new ArrayList<>();
// Loop through each string in the array
for (String str : arr) {
// Increment the count for each string
count.put(str, count.getOrDefault(str, 0) + 1);
}
// Loop through each string in the array again to collect distinct strings
for (String str : arr) {
// If the string appears only once, add it to the distinct list
if (count.get(str) == 1) {
distinct.add(str);
}
}
// Check if the k-th distinct string exists
if (k <= distinct.size()) {
// Return the k-th distinct string (1-based index)
return distinct.get(k - 1);
} else {
// If there are fewer than k distinct strings, return an empty string
return "";
}
}
}