Remove consonants from a string | Practice | GeeksforGeeks

Given a string S, remove all consonants and print the modified string that contains vowels only.
Example 1:
Input
S = "abEkipo"
Output
aEio
Explanation : a, E, i, o are only vowels in the string.

Example 2:
Input
S = "rrty"
Output
No Vowel
Explanation: There are no vowels.


Your Task: You don't need to read input or print anything.Your task is to complete the function removeConsonants() which takes a string as input parameters, and returns the modified string that contains vowels only. If there is no vowel present in the string S, then return "No Vowel".
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(1).
Constraints
1 <= |S| <= 105
The string should consist of only alphabets.
0
vinayreddy222vr1 week ago
python solution
x=""       for i in s:           if (i=='a' or i=='e' or i=='i'or i=='o' or i=='u' or i=='A' or i=='E'or i=='I' or i=='O' or i=='U'):               x=x+i       if(len(x)==0):           return 'No Vowel'       return x 
0
princejee20191 month ago
C++ || EASY TO UNDERSTAND
string removeConsonants(string s){       string res;       for(int i =0;i<s.size();i++){           if(s[i]=='a' || s[i]=='A' || s[i]=='e' || s[i]=='E' || s[i]=='i' ||s[i]=='I' || s[i]=='o'|| s[i]=='O' || s[i]=='u' || s[i]=='U'){               res.push_back(s[i]);           }       }       if(res.size()==0) return "No Vowel";       return res;   }
0
atif836141 month ago
JAVA SOLUTION :
 if (s.length() == 0) {
			return null;
		}
		String ss ="";
		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) == 'A' || s.charAt(i) == 'E' || s.charAt(i) == 'I'
					|| s.charAt(i) == 'O' || s.charAt(i) == 'U') {
				ss = ss + s.charAt(i);
			}


		if(res.length()==0){
		 return "No Vowel";
		    
		}
		return ss;
		}
0
codewithshoaib192 months ago
// Simple use of regex
String x=s.replaceAll("[^aeiouAEIOU]","");       // System.out.println(x);       if(x.equals("")){           return "No Vowel";       }       return x;
0
ankitparashxr2 months ago
java
 String vo = "aeiouAEIOU";       StringBuffer res = new StringBuffer();       for(int i =0;i<s.length();i++)       {           if(vo.indexOf(s.charAt(i))!=-1)           {              res.append(s.charAt(i));           }       }       if(res.length()==0)       {           return "No Vowel";       }       return res.toString();
+1
ciph3rcodes2 months ago
//JAVA SOLN BEST
 
String answer = "No Vowel";boolean check = false;if(s.contains("a") || s.contains("e") || s.contains("i") || s.contains("o")|| s.contains("u") || s.contains("A") || s.contains("E") || s.contains("I")|| s.contains("O") || s.contains("U")){check = true;}
if(check == true){return s.replaceAll("[BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz]", "");}
return answer;
0
arshergon3 months ago
Python3 Solution With O(N) and O(1) Space complexity;
Use a HashSet for O(1) lookUps, List has O(N) lookUps
def removeConsants(s):
	hashSet = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
    res = ""
    for i in s:
    	if i in hashSet:
            res+= i
    return res if res else "No Vowel" 
0
amiransarimy3 months ago
Simple Python Solutions
Total Time Taken:
0.3/1.8

def removeConsonants(self, s):
       vowel_list =['A','E','I','O','U','a','e','i','o','u']
       res =""
       for ch in s:
           if ch in vowel_list:
               res += ch
               
       return "No Vowel" if len(res) == 0 else res
0
ybrijesh6 months ago
 string removeConsonants(string s){   //complete the function heredef removeConsonants(s):          string a;              for(int i=0;i<s.length();i++)       {           if(s[i]=='a' || s[i]=='A' || s[i]=='e' || s[i]=='E' || s[i]=='i' || s[i]=='I' || s[i]=='o' || s[i]=='O' ||           s[i]=='u' ||s[i]=='U')           {               a=a+s[i];           }       }     if(!a.empty())  {      return a;  }  else  {      return "No Vowel";  }      
0
SACHIN KUMAR1 year ago
SACHIN KUMAR
string removeConsonants(string s){    string str;    for(int i=0;i<s.size();i++) {="" if(s[i]="='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U')" {="" str+="s[i];" }="" }="" int="" c="0;" for(int="" i="0;i&lt;str.size();i++)" {="" c++;="" }="" if(c="=0)" {="" return="" "no="" vowel";="" }="" else="" {="" return="" str;="" }="" }<="" u="">
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.