-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexicography.cpp
More file actions
67 lines (63 loc) · 1.47 KB
/
lexicography.cpp
File metadata and controls
67 lines (63 loc) · 1.47 KB
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
57
58
59
60
61
62
63
64
65
66
67
/*You are given a string S of lowercase Latin characters with size N.
Your task is to find the lexicographically smallest substring with the maximum frequency.
Note: String p is lexicographically smaller than string q, if p is a prefix of q, is
not equal to q or there exists i, such that pi < qi and for all j < i it is satisfied
that pj = qj. For example, abc is lexicographically smaller than abcd, abd is
lexicographically smaller than abec, afa is not lexicographically smaller than ab and a
is not lexicographically smaller than a.
Input:
N = 4
S = "gfgg"
Output:
"g"
Explanation:
Substring "g" is present 3
in the string and it can be proved
that it is the lexicographically
smallest one
Example 2:
Input:
N = 1
S = "a"
Output:
"a"
Explanation:
The only possible
substring is "a"*/
#include<iostream>
using namespace std;
string solve(int N, string s)
{
int c[26],fr=-1;
string ans="";
for(int i=0;i<26;i++)
{
c[i]=0;
}
for(int i=0;i<N;i++)
{
- c[s[i]-97]++;
}
for(int i=0;i<26;i++)
{
if(c[i]>0)
{
if(fr==-1||c[i]>c[fr])
{
fr=i;
}
}
}
ans=ans+char(fr+97);
cout<<ans;
}
int main()
{
string s;
int n;
cout<<"\n enter string size:";
cin>>n;
cout<<"\n enter a string:";
cin>>s;
solve(n,s);
}