Kth Distinct String in an Array

Easy
22
43.3% Acceptance

In this lab, you will be implementing a function to find the Kth distinct string in an array. A distinct string is a string that is present only once in an array.

You will be given an array of strings arr, and an integer k, and your task is to return the kth distinct string present in arr. If there are fewer than k distinct strings, your function should return an empty string "".

Note that the strings are considered in the order in which they appear in the array.

For example:

kthDistinct(["d", "b", "c", "b", "c", "a"], 2); // Output: "a" kthDistinct(["aaa", "aa", "a"], 1); // Output: "aaa" kthDistinct(["a", "b", "a"], 3); // Output: ""

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.

Challenges

  1. Implement the kthDistinct function.
  2. Export the kthDistinct function properly.
  3. Test edge cases with different input values.
  4. Make sure the output is correct and follows the given constraints.

In the evaluation script, we will be importing your exported kthDistinct function and testing it against multiple test cases to ensure the correct implementation and desired output.

Good luck and happy coding!