-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisTinct.java
More file actions
89 lines (72 loc) · 2.44 KB
/
DisTinct.java
File metadata and controls
89 lines (72 loc) · 2.44 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
Distinct K
/*You wish to help Ashish, who possesses a collection of N strings, some of which may be duplicated, and has been assigned the task of finding the kth unique string.
If the number of unique strings is less than k, he needs to display an empty string. Considering you are Ashish's best friend can you assist him with this challenge?
Input Format
The first line contains an integer N denoting the number of strings.
The next N lines contain strings.
The next line contains an integer k.
Output Format
The output contains the kth distinct string. If there are less than k unique string display an empty string.
Constraints
1<=N<=105
-10^8<=arr[i].length()<=10^8
Sample Testcase 0
Testcase Input
6
d
b
c
b
c
a
2
Testcase Output
a
Explanation
The only strings in arr that are distinct are "d" and "a." The letter "d" comes first, making it the first separate string.
Because "a" appears second, it is the second distinct string. "a" is returned since k == 2.*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class Solution {
public static String findKthUniqueString(int N, String[] strings, int k) {
if (N == 0 || k <= 0) {
return "";
}
Map<String, Integer> countMap = new HashMap<>();
List<String> uniqueStrings = new ArrayList<>();
// Count occurrences of each string
for (String s : strings) {
countMap.put(s, countMap.getOrDefault(s, 0) + 1);
}
// Collect unique strings that appear exactly once
for (String s : strings) {
if (countMap.get(s) == 1) {
uniqueStrings.add(s);
}
}
// Check if we have enough unique strings
if (uniqueStrings.size() >= k) {
return uniqueStrings.get(k - 1); // k-1 because k is 1-based index
} else {
return "";
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Reading input
int N = scanner.nextInt();
scanner.nextLine(); // Consume newline
String[] strings = new String[N];
for (int i = 0; i < N; i++) {
strings[i] = scanner.nextLine();
}
int k = scanner.nextInt();
// Finding kth unique string
String result = findKthUniqueString(N, strings, k);
System.out.println(result);
scanner.close();
}
}