Skip to main content

2053. Kth Distinct String in an Array

Hint

class Solution {
public:
string kthDistinct(vector<string>& arr, int k)
{
unordered_map<string, int> m;
for (const auto& str : arr) m[str]++;
for (const auto& num : arr)
{
if (m[num] == 1 && !--k) return num;
}
return "";
}
};
  • T: O(n)O(n)
  • S: O(n)O(n)