Kth Distinct String in an Array (LeetCode 2053): Solution and Explanation
A string is distinct if it appears exactly once. Return the k-th distinct string in array order, or an empty string if there are fewer than k.
Intuition
You cannot tell whether a string is distinct when you first meet it. "b" at index 1 looks unique until a second "b" shows up at index 3. Distinctness is a property of the whole array.
So split the work in two. First pass: count every string with a hash map. After that, a lookup tells you instantly whether any string is distinct.
The answer still depends on the original order, so the second pass walks arr from the left again. Each string with count 1 is the next distinct one; count k down and stop when it reaches zero.
Approach: Two passes with a hash map
- Build
count[s]= number of timessoccurs inarr. - Walk
arrfrom left to right. - When
count[s] == 1, decreasek. Ifkbecomes 0, returns. - If the walk ends, there are fewer than k distinct strings: return
"".
Step-by-step example
arr = ["d","b","c","b","c","a"], k = 2
- Pass 1 builds the counts d:1, b:2, c:2, a:1.
- Pass 2: "d" has count 1, it is distinct number 1, k drops to 1.
- "b", "c", "b", "c" all have count 2 and are skipped.
- "a" has count 1, it is distinct number 2, k drops to 0. Return "a".
Code: Python, Java, C++
1class Solution:
2 def kthDistinct(self, arr: List[str], k: int) -> str:
3 count = {}
4 for s in arr:
5 count[s] = count.get(s, 0) + 1
6 for s in arr:
7 if count[s] == 1:
8 k -= 1
9 if k == 0:
10 return s
11 return ""1class Solution {
2 public String kthDistinct(String[] arr, int k) {
3 Map<String, Integer> count = new HashMap<>();
4 for (String s : arr) {
5 count.merge(s, 1, Integer::sum);
6 }
7 for (String s : arr) {
8 if (count.get(s) == 1) {
9 k--;
10 if (k == 0) {
11 return s;
12 }
13 }
14 }
15 return "";
16 }
17}1class Solution {
2public:
3 string kthDistinct(vector<string>& arr, int k) {
4 unordered_map<string, int> count;
5 for (auto& s : arr) {
6 count[s]++;
7 }
8 for (auto& s : arr) {
9 if (count[s] == 1) {
10 k--;
11 if (k == 0) {
12 return s;
13 }
14 }
15 }
16 return "";
17 }
18};Time and space complexity
O(n)
Two passes over the array with O(1) average map operations. Strings are at most 5 characters, so hashing is constant time.
O(n)
The map can hold one entry per string.
Other approaches
Count by scanning (time O(n^2), space O(1))
For each string, scan the whole array to count its copies. Correct for n <= 1000, but quadratic.
Ordered map or LinkedHashMap (time O(n), space O(n))
Keep insertion order in the map and walk its entries instead of arr. Works, but walking arr again is simpler and needs no special map.
Common mistakes
- Deciding distinctness during the first pass, before later duplicates have been counted.
- Walking the hash map instead of arr in the second pass. A plain hash map does not keep the original order.
- Returning null or None instead of the empty string when there are fewer than k distinct strings.
Frequently asked questions
Why does the second pass go over arr and not over the map?
The k-th distinct string is defined by array order. Walking arr keeps that order for free, while most hash maps iterate in an unrelated order.
Can a string with count 2 ever be the answer?
No. Distinct means it occurs exactly once, so any string with a count above 1 is skipped entirely, including its first occurrence.
What if every string is distinct?
Then the answer is simply arr[k - 1], and the two-pass method returns exactly that.