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
+1
rumi1w8ry2 weeks ago
def isGoodorBad(self, s):
       # code here 
       count = 0
       count1 = 0
       v = 'aeiou'
       for i in s:
           if i in v or i == '?':
               count +=1
               if count >5:
                   return 0
           else:
               count =0
           if i not in v or i =='?':
               count1 +=1
               if count1 >3:
                   return 0
           else:
               count1 =0
       return 1
-2
1ashishchauhan20021 month 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;   }
+1
shashankdixit12 months ago
JAVA Solution:
Approach:
Whenever we encounter a vowel, increment vCount and set cCount as 0.
Whenever we encounter a consonant, increment cCount and set vCount as 0 .
The task is to make the string BAD. So, we increment both vCount and cCount when we encounter ‘?’
Time Complexity: O(|S|)
Space Complexity: O(1)
static int isGoodorBad(String s) {
       int vCount=0,cCount=0;
       for(int i=0;i<s.length();i++){
           if(s.charAt(i)=='?'){
               vCount++; cCount++;
           }
           else if(s.charAt(i)=='a' || s.charAt(i)=='e' || s.charAt(i)=='i' || s.charAt(i)=='o' || s.charAt(i)=='u' ){
               cCount=0;
               vCount++;
           }
           else{
               vCount=0;
               cCount++;
           }
           if(cCount>3 || vCount>5){
               return 0;
           }
       }
       return 1;
   }
 
 
0
csedeepak2 months ago
 int isGoodorBad(string S) {       // code here       int vcnt=0,ccnt=0;       int i=0;       while(i<=S.length()){           if(S[i]=='a'|| S[i]=='e'||S[i]=='i'||S[i]=='o'||S[i]=='u'||S[i]=='?'){                  if(vcnt>5)                      return 0;                  vcnt++;                 if(S[i]=='?')                    ccnt++;                else if(ccnt>3)                       return 0;                   else                      ccnt=0;           }           else{               if(ccnt>3)                  return 0;               ccnt++;               if(vcnt>5)                   return 0;               else                  vcnt=0;           }           i++;       }       return 1;   }
0
shubhamjain132 months ago
static int isGoodorBad(String S) {
        List<Character> vowels = 
        Arrays.asList(new Character[]{'a','e','i','o','u','A','E','I','O','U'});
        int vowelsCount = 0;
        int consonantsCount=0;
        if(S.length()>2){
            
            for(int i = 0; i< S.length(); i++){
               
                if(S.charAt(i)=='?'){
                    if(vowelsCount == 5){
                        vowelsCount++;
                    }
                    if(consonantsCount==3){
                        consonantsCount++;
                    }
                }
                
                if(vowelsCount>5 || consonantsCount>3){
                    return 0;
                }
                if(vowels.contains(Character.valueOf(S.charAt(i)))){
                    vowelsCount+=1;
                    consonantsCount = 0;
                }else{
                    consonantsCount+=1;
                    vowelsCount = 0;
                }
                
            }
        }
        return 1;
    }
0
roy99mustang3 months 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.
-5
ashmeetsingh78013 months 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
codewithshoaib193 months 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;   }
-1
user_4na04 months 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;   }};
+1
hitentandon5 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;
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.