2053. Kth Distinct String in an Array - Explanation
Description
A distinct string is a string that is present only once in an array.
You are given an array of strings arr, and an integer k, return the k-th distinct string present in arr. If there are fewer than k distinct strings, return an empty string "".
Note that the strings are considered in the order in which they appear in the array.
Example 1:
Input: arr = ["d","b","c","b","c","a"], k = 2
Output: "a"Explanation: The only distinct strings in arr are "d" and "a".
"d" appears 1st, so it is the 1st distinct string.
"a" appears 2nd, so it is the 2nd distinct string.
Since k == 2,"a" is returned.
Example 2:
Input: arr = ["aaa","aa","a"], k = 1
Output: "aaa"Explanation: All strings in arr are distinct, so the 1st string "aaa" is returned.
Example 3:
Input: arr = ["a","b","a"], k = 3
Output: ""Explanation: The only distinct string is "b". Since there are fewer than 3 distinct strings, we return an empty string "".
Constraints:
1 <= k <= arr.length <= 10001 <= arr[i].length <= 5arr[i]consists of lowercase English letters.
Topics
Prerequisites
Before attempting this problem, you should be comfortable with:
- Hash Maps - Counting occurrences of elements with O(1) average lookup and insertion
- Hash Sets - Tracking unique elements and checking membership efficiently
- Array Traversal - Iterating through arrays while maintaining order
1. Brute Force
Intuition
A string is distinct if it appears exactly once in the array. The simplest approach is to check each string against all other strings. For each position, we scan the entire array to see if that string appears anywhere else. If not, it's distinct, and we decrement our counter until we find the k-th one.
Algorithm
- Iterate through each string at index
i. - For each string, check all other positions
jto see if there's a duplicate. - If no duplicate is found, the string is distinct; decrement
k. - When
kreaches0, return the current string. - If we exhaust all strings without finding
kdistinct ones, return an empty string.
Time & Space Complexity
- Time complexity:
- Space complexity:
2. Hash Map
Intuition
Instead of repeatedly scanning the array, we can count occurrences upfront using a hash map. In the first pass, we count how many times each string appears. In the second pass, we iterate in order and check if each string has a count of exactly 1. This reduces time complexity from O(n^2) to O(n).
Algorithm
- Create a hash map to count occurrences of each string.
- Iterate through the array, incrementing the count for each string.
- Iterate through the array again in order.
- For each string with count equal to
1, decrementk. - Return the string when
kbecomes0, or return an empty string if not found.
Time & Space Complexity
- Time complexity:
- Space complexity:
3. Hash Set
Intuition
We can use two sets instead of a counting map. One set tracks strings that are currently distinct (seen exactly once), and another tracks strings we've already identified as duplicates. When we encounter a string, if it's in the distinct set, we move it to the seen set (it's no longer distinct). If it's not in either set, we add it to distinct. This achieves the same result with a slightly different data structure.
Algorithm
- Create two sets:
distinctfor unique strings andseenfor duplicates. - For each string in the array:
- If it's in
distinct, move it toseen(it's now a duplicate). - If it's not in
seen, add it todistinct.
- If it's in
- Iterate through the array again in order.
- For strings in the
distinctset, decrementk. - Return the string when
kreaches0.
class Solution:
def kthDistinct(self, arr: List[str], k: int) -> str:
distinct, seen = set(), set()
for s in arr:
if s in distinct:
distinct.remove(s)
seen.add(s)
elif s not in seen:
distinct.add(s)
for s in arr:
if s in distinct:
k -= 1
if k == 0:
return s
return ""Time & Space Complexity
- Time complexity:
- Space complexity:
Common Pitfalls
Counting Distinct Strings Out of Order
The k-th distinct string must be found in the original array order, not in any arbitrary order. A common mistake is collecting all distinct strings into a set and then trying to find the k-th one, but sets do not preserve insertion order in all languages. Always iterate through the original array in order when finding the k-th distinct string.
Returning Before Finding the K-th Distinct String
If there are fewer than k distinct strings in the array, the function should return an empty string. Forgetting to handle this edge case or returning the last found distinct string instead of an empty string leads to incorrect results. Always check if k reaches zero before returning, and return empty string if the loop completes without finding enough distinct strings.
Sign in to join the discussion