-
Notifications
You must be signed in to change notification settings - Fork 0
/
kth distinct string element.txt
56 lines (27 loc) · 1.24 KB
/
kth distinct string element.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Given an array of strings arr, and an integer k, return the kth distinct string present in arr. If there are fewer than k distinct strings, return an empty string "".
Note that the strings are considered in the order in which they appear in the array.
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.
class Solution {
public:
string kthDistinct(vector<string>& arr, int k) {
map <string,int> save;
//save each element of array in map (so I know which one is a distinct string and which one not)
for (auto s : arr)save[s]++;
for (auto s : arr){
//start from the first element of array we check if it is a distinct string, if so k--
//so we can get kth disitinct string
if (save[s] == 1)k--;
//when k = 0 means that we find the kth disitinct string
if (k == 0)return s;
}
//if the loop is finished and we didn't returned anything (so means that k != 0) means that there are fewer than k distinct strings
return "";
}
};