Reversing the vowels | Practice | GeeksforGeeks

Given a string consisting of lowercase english alphabets, reverse only the vowels present in it and print the resulting string.
Example 1:
Input:
S = "geeksforgeeks"
Output: geeksforgeeks
Explanation: The vowels are: e, e, o, e, e
Reverse of these is also e, e, o, e, e.


Example 2:
Input: 
S = "practice"
Output: prectica
Explanation: The vowels are a, e
Reverse of these is e, a.

 
Example 3:
Input: 
S = "bcdfg"
Output: bcdfg
Explanation: There are no vowels in S.

Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes the string S as input and returns the resultant string by reversing only the vowels present in S.

Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).

Constraints:
1<=|S|<=105
 
0
sayanmazumder9992 weeks ago
JAVA---0.66/7.5 Seconds--100% Accuracy
    StringBuilder str = new StringBuilder();   StringBuilder STR = new StringBuilder(s);   int j = 0; char c;      // catch all the vowels inside s.   for(int  i = 0; i <s.length(); i++) {   c = s.charAt(i);   if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')  str.append(c); }   str = str.reverse();      // place those vowels in reversed form.   for(int  i = 0; i < STR.length(); i++) {   c = STR.charAt(i);   if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') STR.setCharAt(i, str.charAt(j++)); }     return String.valueOf(STR);
0
ishashi2 weeks ago
    String modify (String s)   {       char[] l = s.toCharArray();       StringBuilder str = new StringBuilder();       String v ="aeiou";       for (int i = 0; i<l.length; i++){           if(v.contains(String.valueOf(l[i]))){               str.append(String.valueOf(l[i]));           }       }       String r = str.reverse().toString();       int j =0;       StringBuilder n = new StringBuilder();       for (int i = 0; i<l.length; i++){           if(v.contains(String.valueOf(l[i]))){               n.append(String.valueOf(r.charAt(j)));               j = j+1;           }           else{               n.append(String.valueOf(l[i]));           }       }       return n.toString() ;          }
0
ishashi
This comment was deleted.
0
somiljaincse252 weeks ago
#JAVA SOLUTION
class Solution{   String modify (String s)   {       // your code here       char[] str=s.toCharArray();       Stack<Character> st=new Stack<Character>();       for(int i=0;i<str.length;i++){           if(str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u'){               st.push(str[i]);               str[i]='1';           }       }       for(int i=0;i<str.length;i++){           if(str[i]=='1'){               str[i]=st.peek();               st.pop();           }       }       return String.valueOf(str);                   }}
0
manavkanjiyanijobupdate2 weeks ago
--------------JAVA SOLUTION--------------------------ALL TEST CASE PASS
class Solution{   String modify (String s)   {       int[] index = new int[s.length()];        char[] afterChange = new char[s.length()];        int count = 0;       char[] ch = s.toCharArray();      for(int i = 0 ; i < ch.length ; i++)      {          if(ch[i] == ('a') || ch[i] == ('i') || ch[i] == ('o') || ch[i] == ('u') || ch[i] == ('e'))          {               index[count] = i;               afterChange[count] = ch[i];                count++;          }      }           StringBuilder  sb = new StringBuilder(String.valueOf(afterChange).substring(0,count));             char[] sr =sb.reverse().toString().toCharArray();      for(int i = 0; i < sr.length;i++)      {                             ch[index[i]] = sr[i];      }      return String.valueOf(ch);         }}
0
neelasatyasai931 month ago
#PYTHON    (accuracy is 16%)     Time 0.6/9.0
class Solution:   def modify(self, s):     ove=""     j=0     for i in s:         if (i == 'a' or i=='e' or i=='i' or i=='o' or i=='u') :               ove+=i     c=ove[::-1]      new=''     for i in range(len(s)):         if (s[i] == 'a' or s[i]=='e' or s[i]=='i' or s[i]=='o' or s[i]=='u') :               new=new+c[j]               j+=1         else:               new=new+s[i]     return new
+1
priyaranjandash1 month ago
                ********Java Solution************
class Solution{   String modify (String s)   {       String res="";  char s1[] = s.toCharArray();  for(int i=0;i<s1.length;i++) {   if(s1[i] == 'a' || s1[i] == 'e' || s1[i] == 'i' || s1[i] == 'o' || s1[i] == 'u') {    res+=s1[i];    s1[i]='@';   }  }  int n=res.length() -1;  for(int i=0;i<s1.length;i++) {   if(s1[i] == '@') {    s1[i]= res.charAt(n);    n--;   }  }    String s2=String.valueOf(s1);  return s2;   }
+1
chauhankartikkc132 months ago
this problem is medium level , why it is put in school level
0
alidikme012 months ago
c++ 
 
string modify (string s)       {           string tempt="";           string tempt2="";           int index_start=0;           int index_last=s.size();           string vowels="aeoui";           while(index_start <= index_last){               if(vowels.find(s[index_last])== std::string::npos){                   index_last-=1;               }else if(vowels.find(s[index_start]) == std::string::npos){                   index_start+=1;               }else if(vowels.find(s[index_start]) != std::string::npos && vowels.find(s[index_last] != std::string::npos)){                   tempt=s[index_start];                   tempt2=s[index_last];                   s.replace(index_start,1,tempt2);                   s.replace(index_last,1,tempt);                   index_start++;                   index_last--;               }           }           return s;       }
0
shahabuddinbravo402 months ago
class Solution{   public:       string modify (string s)       {           int i;           string temp;           for(i=0;i<s.size();i++){               if(s[i]=='a' || s[i]=='e' || s[i]=='o' || s[i]=='i'|| s[i]=='u'){                   temp.push_back(s[i]);               }           }           int k=0;           for(i=0;i<s.size();i++){               if(s[i]=='a' || s[i]=='e' || s[i]=='o' || s[i]=='i'|| s[i]=='u'){                   s[i]=temp[temp.size()-1-k];                   k++;               }           }           return s;       }};
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.