Sherlock and the Valid String

  • + 0 comments

    Through Brute Force:> cpp solution

    string isValid(string s) { int freq[26]={0}; for (char c:s) { freq[c-'a']++; }

    sort(freq,freq+26);
    
    if ((freq[24]==0) && (freq[25]!=0))
    {
        return "YES";
    }
    int j=0;
    while(freq[j]==0)
    {
        j++;
    }
    if (freq[j]==freq[25])
    {
        return "YES";
    }
    
    else if ((freq[j]==1) &&  freq[j+1]==freq[25])
    {
        return "YES";
    }
    
    else if((freq[25]-freq[j]==1) && freq[j]==freq[24])
    {
        return "YES";
    }
    
    else
    {
        return "NO";
    }
    

    }