Skip to content

Commit 6826e2b

Browse files
committed
changes_commited
1 parent 2807604 commit 6826e2b

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include<iostream>
2+
#include<set>
3+
using namespace std;
4+
// https://www.geeksforgeeks.org/find-kth-distinct-character-from-start-of-given-given-string/
5+
6+
// Function for printing the Kth distinct character using sets
7+
int printKthDistinct(string& str, int& k)
8+
{
9+
//Declaring a set named uniqueEle
10+
set<int> uniqueEle;
11+
int n = str.length();
12+
//Traversing the given string and finding the kth unique/ distinct element
13+
for (int i = 0; i < n; i++) {
14+
uniqueEle.insert(str[i]);
15+
if (uniqueEle.size() == k)
16+
return str[i];
17+
}
18+
return -1;
19+
}
20+
21+
int main()
22+
{
23+
//taking the input from the user
24+
string str;
25+
cout<<"Enter the string: ";
26+
cin>>str;
27+
int k;
28+
cout<<endl<<"Enter the value of k: ";
29+
cin>>k;
30+
int ans = printKthDistinct(str, k);
31+
if (ans == -1)
32+
cout << -1;
33+
else
34+
cout << (char)ans;
35+
return 0;
36+
}
37+
38+
//Time Complexity: O(N * N)
39+
//Auxiliary Space: O(1)
79.2 KB
Binary file not shown.

hacktoberfest

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 280760463a3410dee2e0c7b83803e3f46da9e7fa

0 commit comments

Comments
 (0)