Question:-
Chef Tobby is playing a rapid fire with Bhuvan. He gives Bhuvan a string S and each time, Bhuvan has to guess whether there exist 2 equal subsequences in the string or not.
Bhuvan got a perfect score in the game with Chef Tobby. However, Chef Tobby has now asked Bhuvan to write a program that will do this automatically given a string S. Bhuvan is an intelligent man but he does not know how to write a code. Can you help him?
Find two different subsequences such that they are equal in their value, more formally, find two sequences of indices (a1, a2, ..., ak-1, ak) and (b1, b2, ..., bk-1, bk) such that:
- 1≤ ai, bi ≤ |S|
- ai < ai+1 for all valid i
- bi < bi+1 for all valid i
- Sai = Sbi for all valid i
- there exist at least one i such that ai is not equal to bi .
Input section
The first line contains T, the number of test cases.
Each of the next T lines contains one string S each.
Input will only consist of lowercase English characters
Output section
For each test case, output "yes" or "no" (without quotes) as the solution to the problem.
Solution:
- #include <stdio.h> #include <string.h> int main() { int t,i,len,flag; char str[101]; scanf("%d",&t); int arr[26]; while(t--) { flag=0; for(i=0;i<26;i++) arr[i]=0; scanf("%s",str); len=strlen(str); for(i=0;i<len;i++) { arr[str[i]-'a']++; if(arr[str[i]-'a']==2) { flag=1; break; } } if(flag==1) printf("yes\n"); else printf("no\n"); } return 0; }
No comments:
Post a Comment