2053. Kth Distinct String in an Array LeetCode Solution
In this guide, you will get 2053. Kth Distinct String in an Array LeetCode Solution with the best time and space complexity. The solution to Kth Distinct String in an Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Kth Distinct String in an Array solution in C++
- Kth Distinct String in an Array solution in Java
- Kth Distinct String in an Array solution in Python
- Additional Resources

Problem Statement of Kth Distinct String in an Array
A distinct string is a string that is present only once in an array.
Given an array of strings arr, and an integer k, return the kth 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 <= 1000
1 <= arr[i].length <= 5
arr[i] consists of lowercase English letters.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
2053. Kth Distinct String in an Array LeetCode Solution in C++
class Solution {
public:
string kthDistinct(vector<string>& arr, int k) {
unordered_map<string, int> count;
for (const string& a : arr)
++count[a];
for (const string& a : arr)
if (count[a] == 1 && --k == 0)
return a;
return "";
}
};
/* code provided by PROGIEZ */
2053. Kth Distinct String in an Array LeetCode Solution in Java
class Solution {
public String kthDistinct(String[] arr, int k) {
Map<String, Integer> count = new HashMap<>();
for (final String a : arr)
count.merge(a, 1, Integer::sum);
for (final String a : arr)
if (count.get(a) == 1 && --k == 0)
return a;
return "";
}
}
// code provided by PROGIEZ
2053. Kth Distinct String in an Array LeetCode Solution in Python
class Solution:
def kthDistinct(self, arr: list[str], k: int) -> str:
count = collections.Counter(arr)
for a in arr:
if count[a] == 1:
k -= 1
if k == 0:
return a
return ''
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.