Kth Distinct String in an Array

Updated on 03 June, 2025
Kth Distinct String in an Array header image

Problem Statement

In this task, you are given an array of strings arr and an integer k. You need to identify the kth distinct string in the array where a "distinct" string is defined as one that appears only once within the list. The search for distinct strings should respect the original order in which strings appear in the array. If the number of unique strings in the array is less than k, the function should return an empty string "". This requirement invites the need to count occurrences of each string, identify the distinct ones, and then determine their order of first appearance up to the kth position.

Examples

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.

Approach and Intuition

The problem requires identifying strings within an array that are unique and then retrieving the kth one in the order they first appear. Here's how this can be approached:

  1. Traverse the array and use a dictionary to count occurrences of each string.
  2. Filter the dictionary to only include strings that occur once, signifying they are distinct.
  3. Traverse the array again and for each string that appears in the filtered dictionary, keep track of its position.
  4. Once we get to the kth distinct string during this second pass, capture and return that string.
  5. If the count of distinct strings is less than k by the end of this traversal, return an empty string.

Let's explore this concept with the provided examples:

  • In the first example, the array has largely repeated items with just two unique strings "d" and "a". Identifying the second distinct string brings us "a".
  • The second example presents straightforwardly with no repetitions — the first string is our answer.
  • The third example checks for robustness by asking for more distinct strings than are available, rightly resulting in an empty return.

These steps address not only the mechanism for filtering and counting but also the nuances in array traversal and condition checks based on k, making it clear how computational complexity and efficiency are incorporated given the constraints. This method ensures that the required operations are efficient and clear, adhering to the order of elements as a significant part of the solution.

Solutions

  • C++
  • Java
  • Python
cpp
class Solution {
public:
    string kthUnique(vector<string>& arr, int k) {
        unordered_map<string, int> count;

        // First loop to count frequency of each string
        for (string& s : arr) {
            count[s]++;
        }

        // Second loop to locate the k-th unique string
        for (string& s : arr) {
            // Decrement k if the string appears only once
            if (count[s] == 1) {
                k--;
            }
            // Return the string when k is 0
            if (k == 0) {
                return s;
            }
        }

        return "";
    }
};

Below is a solution summary for the problem of finding the k-th distinct string in an array using C++:

The provided C++ code defines a method kthUnique, which finds the k-th unique string in a given array of strings.

  • The solution utilizes the unordered_map to count the occurrences of each string in the array.
  • The process encompasses two main loops:
    • The first loop iterates through the array arr, using the map count to record the frequency of each string.
    • The second loop again iterates through arr, this time checking for strings that appear exactly once. The variable k is decremented each time a unique string is found.
    • If k reaches zero, the current string, which is the k-th unique string, is returned.

If no such k-th unique string exists (i.e., k is not zero after completing the second loop), the function returns an empty string as the default output.

This method ensures that the first occurrence of each unique string is considered for the count toward k, aligning with its first appearance in the list order from the input array.

java
class Solution {

    public String findKthDistinct(String[] array, int k) {
        Map<String, Integer> countMap = new HashMap<>();

        // First loop to count occurrences
        for (String element : array) {
            countMap.put(element, countMap.getOrDefault(element, 0) + 1);
        }

        // Second loop to identify the k-th unique element
        for (String element : array) {
            // Identify unique elements
            if (countMap.get(element) == 1) {
                k--;
            }
            // Outcome when k is zero
            if (k == 0) {
                return element;
            }
        }

        return "";
    }
}

The provided Java solution finds the k-th distinct string in an array. It uses a HashMap to count the occurrences of each string in the array. Here's how the solution workflow is structured:

  • Count Frequency of Each Element:

    • Iterate through all elements in the input array.
    • Update the frequency count of each string using the HashMap (countMap). If the string is not in the map, initialize its count to 0, then increment by 1.
  • Identify and Return the K-th Distinct String:

    • Iterate through the array again.
    • For each element, check if its count in the HashMap is equal to 1, indicating it is unique.
    • Decrement k each time a unique string is encountered.
    • When the decremented value of k reaches 0, the current element is the k-th distinct string. Return this string.
  • Handle Non-existence Case:

    • If the function exhausts all the elements in the array and can't find the k-th distinct string, return an empty string.

This approach efficiently tracks and retrieves the k-th distinct string by leveraging the counting map creation and subsequent scanning, supporting cases where some strings may not be distinct. Also, it ensures that the impact of multiple loops is mitigated by not rechecking already identified distinct strings. Hence, it provides a way to find the needed value without excessive computational overhead.

python
class Solution:
    def findKthDistinct(self, words: List[str], index: int) -> str:
        count_dict = {}

        # Fill dictionary with counts
        for word in words:
            count_dict[word] = count_dict.get(word, 0) + 1

        # Traverse to find the k-th unique element
        for word in words:
            if count_dict[word] == 1:  # Distinct element
                index -= 1
            if index == 0:  # Desired position reached
                return word

        return ""  # Return empty string if no k-th distinct element found

The Python solution provided addresses the problem of finding the kth distinct string in an array. Here's a breakdown of how the solution works:

  • It begins by creating a dictionary to store the frequency of each word in the array.

  • The solution iterates over the list of words, incrementing the word's count in the dictionary each time it appears in the list.

  • It then processes the list again to check for distinct words (words that appear exactly once in the array).

  • While traversing the words the second time, the script decrements the given index each time a distinct word is encountered. When the index reaches zero, it implies that we've reached the kth distinct element, and the function returns the word at that position.

  • If the index does not reach zero (meaning there aren't enough distinct words), the function returns an empty string.

This approach efficiently provides the kth distinct word by using a dictionary for counting and a simple iteration to retrieve the desired element based on its distinct count. The complexity mainly depends on the size of the input list for both space and time since each element is processed and stored in the dictionary.

Comments

No comments yet.