Good or Bad string | Practice | GeeksforGeeks

In this problem, a String S is composed of lowercase alphabets and wildcard characters i.e. '?'. Here, '?' can be replaced by any of the lowercase alphabets. Now you have to classify the given String on the basis of following rules:
If there are more than 3 consonants together or more than 5 vowels together, the String is considered to be "BAD". A String is considered "GOOD" only if it is not “BAD”.
NOTE: String is considered as "BAD" if the above condition is satisfied even once. Else it is "GOOD" and the task is to make the string "BAD".
 
Example 1:
Input:
S = aeioup??
Output:
1
Explanation: The String doesn't contain more
than 3 consonants or more than 5 vowels together.
So, it's a GOOD string.

Example 2:
Input:
S = bcdaeiou??
Output:
0
Explanation: The String contains the
Substring "aeiou??" which counts as 7
vowels together. So, it's a BAD string.



Your Task:
You don't need to read input or print anything. Your task is to complete the function isGoodorBad() which takes the String S as input and returns 0 or 1.
 
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= |S| <= 100000
0
roy99mustang2 days ago
Easy Python solution: 
#User function Template for python3

class Solution:
    def isGoodorBad(self, S):
        v = "aeiou"
        
        def isV(p):
            return all([(x in v or x is "?") for x in p])
            
        def isC(p):
            return all([(x not in v or x is "?") for x in p])
            
        j = 0
        n = len(S)
        while j < n:
            # check for a bad substring
            if (j + 5 < n and isV(S[j:j+6])) or (j + 3 < n and isC(S[j:j+4])):
                return 0
            else:
                j += 1
        return 1
        
#{ 
#  Driver Code Starts
#Initial Template for Python 3

if __name__ == '__main__': 
    t = int (input ())
    for _ in range (t):
        S=input()
        
        ob = Solution()
        print(ob.isGoodorBad(S))
# } Driver Code Ends
The question marks, “?”, have to be treated as vowels or consonants strategically; so as to force a bad substring.
0
ashmeetsingh78013 days ago
int isGoodorBad(string S)    {       int v=0,c=0;       for(int i=0;i<S.size();i++)       {           if(S[i]=='a' ||S[i]=='e' ||S[i]=='i' ||S[i]=='o' ||S[i]=='u' )           {               v++;               c=0;           }           else           {               c++;               v=0;           }           if(c>3 || v>5) return 0;       }       return 1;   }
0
codewithshoaib191 week ago
// Simple easy to understand java solution
static int isGoodorBad(String S) {              // finding index of ?       int x=S.indexOf("?");              // if character before ? is consonant then change all ? with any consonants              if(x!=-1 && x-1>0 && !(isVowel(S.charAt(x-1)))){       S=S.replaceAll("[^a-zA-Z]","g");       }              // if character before ? is vowels then change all ? with any vowel              if(x!=-1 && x-1>0 && (isVowel(S.charAt(x-1)))){       S=S.replaceAll("[^a-zA-Z]","e");       }       // iterating over the string              for(int i=0;i<S.length();i++){           char ch=S.charAt(i);                      // check wheter a char is vowel is vowel or consonant                      // if vowel then point j on it and increment it till it reaches more than 5                      if(isVowel(ch)){               int j=i;               int k=0;               while(j<S.length() && isVowel(S.charAt(j))){                   j++;                   k++;                   // if it becomes greater than 5 then break and return false                   if(k>5){                       // System.out.println(k);                       return 0;                   }               }           }           // if consonant then point j on it and increment it till it reaches more than 3           else if(!isVowel(ch)){               int j=i;               int k=0;               while(j<S.length() && !isVowel(S.charAt(j))){                   j++;                   k++;                    // if it becomes greater than 3 then break and return false                   if(k>3){                       // System.out.println(k);                       return 0;                   }               }           }                  }       return 1;   }   public static boolean isVowel(char ch){       if(ch=='a' || ch=='e' || ch=='o' || ch=='i' || ch=='u'){           return true;       }       return false;   }
0
user_4na01 month ago
class Solution { public:   int isGoodorBad(string S) {       int con = 0,vow = 0;       int cnt = 0;       for(int i = 0;i<S.length();i++)       {           if(S[i] == 'a' || S[i] == 'e' || S[i] == 'o' || S[i] == 'i' || S[i] == 'u')           {               if(vow>5)               {                   cnt++;                   vow = 0;               }               con = 0;               vow++;           }           else           {               if(con == 3)               {                   cnt++;                   con = 0;               }               vow = 0;               con++;           }       }       return (cnt>0)?0:1;   }};
0
hitentandon2 months ago
Java Sol:
	int vc = 0, cc= 0;
        for(char c: S.toCharArray())
            if(vc>5||cc>3)
                return 0;
            else if(c=='?')
            {
                vc++;
                cc++;
            }
            else if("AEIOUaeiou".indexOf(c)>-1)
            {
                vc++;
                cc=0;
            }   
            else
            {
                cc++;
                vc=0;
            }
        return 1;
0
mayank20213 months ago
C++
 
int isGoodorBad(string S) {
  int countc=0, countv=0;  for(int i=0; i<S.size(); i++)  {      if(S[i]=='a' || S[i]=='e' || S[i]=='i' || S[i]=='o' || S[i]=='u'  )        {            countv++;            countc=0;            if(countv>5)              return 0;                    }              else if (S[i]=='?')        {           countv++;           countc++;           if(countv>5 || countc >3)              return 0;                    }       else        {            countc++;            countv=0;            if(countc>3)             return 0;        }              }    return 1;
   }
0
himanshujain457
This comment was deleted.
0
anmol1neema4 months ago
int k=0,j=0;       String s = S.replaceAll(" ","");             for(int i=0;i<s.length();i++){          if(s.charAt(i)=='a'||s.charAt(i)=='e'||          s.charAt(i)=='i'||s.charAt(i)=='o'||s.charAt(i)=='u'          || s.charAt(i)=='?'){              if(k>3){                  break;              }              k=0;              j++;          }else{              if(j>5){                  break;              }              j=0;              k++;          }      }      if(j>5 || k>3){         return 1;      }else{          return 0;      }         }
+3
aditichoudharyit194 months ago
class Solution {
  public:
    int isGoodorBad(string S) {
      int vowel = 0 ;
      int cons = 0 ;
      for( int i = 0 ; i < S.length() ; i++ )
       {
          if( vowel > 5 || cons > 3 )
           {
             return 0;
           }
          if( S[i] == 'a' || S[i] == 'e' || S[i] == 'i' || S[i] == 'o' || S[i] == 'u' )
           {
              vowel++ ;
              cons = 0 ;
           }
          else if( S[i] == '?') 
            {
              cons++;
              vowel++;
            }
           else 
            {
             vowel = 0;
              cons++;
            }
          
      }
        
          return 1;
    }
};
+1
badgujarsachin837 months ago
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.