-
Notifications
You must be signed in to change notification settings - Fork 111
/
solution.cpp
52 lines (47 loc) · 934 Bytes
/
solution.cpp
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
/*Problem link:-
https://www.hackerrank.com/challenges/sherlock-and-valid-string
*/
#include<bits/stdc++.h>
using namespace std;
int main() {
int nl,l,i;
int count=0;
int arr[26]={0};
char str[100000];
//Taking input from user
cin>>str;
l=strlen(str);
//updating the auxiliary array 'arr' with no. of occurances
for(i=0;i<l;i++)
{
int x=str[i]-97;
arr[x]++;
}
l=0;
//sorting occurances in ascending order
sort(arr,arr+26);
for(i=0;i<26;i++)
{
if(arr[i]==0)
l++;
}
if(arr[l]==1)
{
l++;
count++;
}
// updating the relative count of characters
for(i=l+1;i<26;i++)
{
if(arr[i]!=arr[l])
{
count++;
}
}
//result printing
if(count>1)
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
return 0;
}