I cannot solve this problem (LCH15JAB)

#include < iostream >
#include < string >

using namespace std;

int main(){

int T, i, j, count=0, flag=0;
string S;

cin>>T;

while(T--){
	
	cin>>S;
	
	for(i=0;i<S.length();i++){
		
		for(j=i;j<S.length();j++){
				
			if(S[i]==S[j])
				count++;
			
		}
		
		if(S.length()==(count*2)){
				
			flag=1;
			break;
			
		}
		
		count=0;
		
	}
	
	if(flag==1)
		cout<<"YES\n";
	else
		cout<<"NO\n";
	
	flag=0;
	count=0;
	
}

}

This code doesn’t work and I can’t figure out why. Please help.

/* package codechef; // don’t place package name! */

import java.util.;
import java.lang.
;
import java.io.*;

/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner s=new Scanner(System.in);
int t=s.nextInt();

	while(t-->0)
	{
	    String st=s.next();
	    //hashing
	    int []a=new int[26];
	    for(int i=0;i<26;i++)
	    a[i]=0;
	    int ind=0;
	    int max=Integer.MIN_VALUE;
	    for(int i=0;i<st.length();i++)
	    {
	        int index=st.charAt(i)-'a';
	        a[index]++;
	        if(a[index]>max)
	        {
	        max=a[index];
	        ind=index;
	        }
	    }
	    int sum=0;
	    for(int i=0;i<26;i++)
	    {
	        if(i!=ind)
	        sum=sum+a[i];
	    }
	    if(sum==max)
	    System.out.println("YES");
	    else
	    System.out.println("NO");
	}
	s.close();
}

}

Hope this helps you ,
Happy coding

1 Like