mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
cs: add «2053. Kth Distinct String in an Array»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
d36f1554da
commit
68d1ba3fff
1 changed files with 21 additions and 0 deletions
21
cs/kth-distinct-string-in-an-array.cs
Normal file
21
cs/kth-distinct-string-in-an-array.cs
Normal file
|
@ -0,0 +1,21 @@
|
|||
public class Solution {
|
||||
public string KthDistinct(string[] arr, int k) {
|
||||
var uniqueWords = arr.ToHashSet();
|
||||
var freqs = uniqueWords.ToDictionary(key => key, key => arr.Count(w => w == key));
|
||||
|
||||
var (i, found) = (0, 0);
|
||||
for (; i < arr.Length && found < k; ++i) {
|
||||
if (freqs[arr[i]] != 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
++found;
|
||||
}
|
||||
|
||||
if (found < k) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return arr[i - 1];
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue