Kth Distinct String in an Array (LeetCode 2053): Solution and Explanation

Easy
Array
Hash table
String
Counting

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

  1. Build count[s] = number of times s occurs in arr.
  2. Walk arr from left to right.
  3. When count[s] == 1, decrease k. If k becomes 0, return s.
  4. 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
  1. Pass 1 builds the counts d:1, b:2, c:2, a:1.
  2. Pass 2: "d" has count 1, it is distinct number 1, k drops to 1.
  3. "b", "c", "b", "c" all have count 2 and are skipped.
  4. "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 ""
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Two passes over the array with O(1) average map operations. Strings are at most 5 characters, so hashing is constant time.

Space

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.