#2053

Kth Distinct String in an Array

newbie · 150 · lc easy +14 · verified · 82.1% accepted · 1,314 likes · top 93%

Description

A string is "distinct" in arr if it appears exactly once. Given the array arr and integer k, return the k-th distinct string (in the order strings appear in arr). If fewer than k distinct strings exist, return "".

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 "".

Code

1
2
3