Reversing the vowels in a String

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
Given a string, reverse only the vowels present in it and print the
resulting string.
Input:
First line of the input file contains an integer T denoting the
number of test cases. Then T test cases follow. Each test case has a
single line containing a string.
Output:
Corresponding to each test case, output the string with vowels
reversed.
Example:
Input:
4
geeksforgeeks
practice
wannacry
ransomware
Output:
geeksforgeeks
prectica
wannacry
rensamwora
My approach:
/*package whatever //do not write package name here */
import java.util.Scanner;
import java.io.IOException;
import java.lang.StringBuffer;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
class GFG
private static String reverseVowels (String str)
List <Character>vowels = new ArrayList <>();
vowels.add('a');
vowels.add('e');
vowels.add('i');
vowels.add('o');
vowels.add('u');
List <Character>values = new ArrayList <>();
List <Integer>keys = new ArrayList <>();
for (int i = 0; i < str.length(); i++)
if (vowels.contains(str.charAt(i)))
keys.add(i);
values.add(str.charAt(i));
StringBuffer sb = new StringBuffer(str);
Collections.reverse(values);
int count = 0;
for (Integer num : keys)
sb.replace(num.intValue(),num.intValue() + 1,String.valueOf(values.get(count)));
count++;
return sb.toString();
public static void main (String args) throws IOException
Scanner sc = new Scanner (System.in);
int numTests = sc.nextInt();
for (int i = 0; i < numTests; i++)
String str = sc.next();
System.out.println(reverseVowels(str));
I have the following questions about my code:
1) How can I further improve my approach?
2) Is there a better way to solve this question?
3) Are there any grave code violations that I have committed?
4) Can space and time complexity be further improved?
My time complexity is O(n) and space complexity is also O(n) currently.
Reference
java beginner strings interview-questions complexity
add a comment |Â
up vote
0
down vote
favorite
Given a string, reverse only the vowels present in it and print the
resulting string.
Input:
First line of the input file contains an integer T denoting the
number of test cases. Then T test cases follow. Each test case has a
single line containing a string.
Output:
Corresponding to each test case, output the string with vowels
reversed.
Example:
Input:
4
geeksforgeeks
practice
wannacry
ransomware
Output:
geeksforgeeks
prectica
wannacry
rensamwora
My approach:
/*package whatever //do not write package name here */
import java.util.Scanner;
import java.io.IOException;
import java.lang.StringBuffer;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
class GFG
private static String reverseVowels (String str)
List <Character>vowels = new ArrayList <>();
vowels.add('a');
vowels.add('e');
vowels.add('i');
vowels.add('o');
vowels.add('u');
List <Character>values = new ArrayList <>();
List <Integer>keys = new ArrayList <>();
for (int i = 0; i < str.length(); i++)
if (vowels.contains(str.charAt(i)))
keys.add(i);
values.add(str.charAt(i));
StringBuffer sb = new StringBuffer(str);
Collections.reverse(values);
int count = 0;
for (Integer num : keys)
sb.replace(num.intValue(),num.intValue() + 1,String.valueOf(values.get(count)));
count++;
return sb.toString();
public static void main (String args) throws IOException
Scanner sc = new Scanner (System.in);
int numTests = sc.nextInt();
for (int i = 0; i < numTests; i++)
String str = sc.next();
System.out.println(reverseVowels(str));
I have the following questions about my code:
1) How can I further improve my approach?
2) Is there a better way to solve this question?
3) Are there any grave code violations that I have committed?
4) Can space and time complexity be further improved?
My time complexity is O(n) and space complexity is also O(n) currently.
Reference
java beginner strings interview-questions complexity
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Given a string, reverse only the vowels present in it and print the
resulting string.
Input:
First line of the input file contains an integer T denoting the
number of test cases. Then T test cases follow. Each test case has a
single line containing a string.
Output:
Corresponding to each test case, output the string with vowels
reversed.
Example:
Input:
4
geeksforgeeks
practice
wannacry
ransomware
Output:
geeksforgeeks
prectica
wannacry
rensamwora
My approach:
/*package whatever //do not write package name here */
import java.util.Scanner;
import java.io.IOException;
import java.lang.StringBuffer;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
class GFG
private static String reverseVowels (String str)
List <Character>vowels = new ArrayList <>();
vowels.add('a');
vowels.add('e');
vowels.add('i');
vowels.add('o');
vowels.add('u');
List <Character>values = new ArrayList <>();
List <Integer>keys = new ArrayList <>();
for (int i = 0; i < str.length(); i++)
if (vowels.contains(str.charAt(i)))
keys.add(i);
values.add(str.charAt(i));
StringBuffer sb = new StringBuffer(str);
Collections.reverse(values);
int count = 0;
for (Integer num : keys)
sb.replace(num.intValue(),num.intValue() + 1,String.valueOf(values.get(count)));
count++;
return sb.toString();
public static void main (String args) throws IOException
Scanner sc = new Scanner (System.in);
int numTests = sc.nextInt();
for (int i = 0; i < numTests; i++)
String str = sc.next();
System.out.println(reverseVowels(str));
I have the following questions about my code:
1) How can I further improve my approach?
2) Is there a better way to solve this question?
3) Are there any grave code violations that I have committed?
4) Can space and time complexity be further improved?
My time complexity is O(n) and space complexity is also O(n) currently.
Reference
java beginner strings interview-questions complexity
Given a string, reverse only the vowels present in it and print the
resulting string.
Input:
First line of the input file contains an integer T denoting the
number of test cases. Then T test cases follow. Each test case has a
single line containing a string.
Output:
Corresponding to each test case, output the string with vowels
reversed.
Example:
Input:
4
geeksforgeeks
practice
wannacry
ransomware
Output:
geeksforgeeks
prectica
wannacry
rensamwora
My approach:
/*package whatever //do not write package name here */
import java.util.Scanner;
import java.io.IOException;
import java.lang.StringBuffer;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
class GFG
private static String reverseVowels (String str)
List <Character>vowels = new ArrayList <>();
vowels.add('a');
vowels.add('e');
vowels.add('i');
vowels.add('o');
vowels.add('u');
List <Character>values = new ArrayList <>();
List <Integer>keys = new ArrayList <>();
for (int i = 0; i < str.length(); i++)
if (vowels.contains(str.charAt(i)))
keys.add(i);
values.add(str.charAt(i));
StringBuffer sb = new StringBuffer(str);
Collections.reverse(values);
int count = 0;
for (Integer num : keys)
sb.replace(num.intValue(),num.intValue() + 1,String.valueOf(values.get(count)));
count++;
return sb.toString();
public static void main (String args) throws IOException
Scanner sc = new Scanner (System.in);
int numTests = sc.nextInt();
for (int i = 0; i < numTests; i++)
String str = sc.next();
System.out.println(reverseVowels(str));
I have the following questions about my code:
1) How can I further improve my approach?
2) Is there a better way to solve this question?
3) Are there any grave code violations that I have committed?
4) Can space and time complexity be further improved?
My time complexity is O(n) and space complexity is also O(n) currently.
Reference
java beginner strings interview-questions complexity
asked Jul 18 at 18:51
Anirudh Thatipelli
42229
42229
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
Time complexity is $O(n)$ indeed. Since every input character must be inspected, this asymptotic is impossible to improve. However, it is possible to improve the constant. See below.
Space complexity is $O(n)$ indeed. However, it is possible to solve the problem is $O(1)$. Consider two iterators, one running left to right, and another running right to left. As long as the first iterator points a consonant, print it. As soon as it hits a vowel, scan the second iterator (right to left) until it finds a vowel, and print that vowel. Notice that this approach also doesn't need
valuesandkeys- and hence doesn't need to build them. This is how time complexity is also improved.The line
sb.replace(num.intValue(),num.intValue() + 1,String.valueOf(values.get(count)));is very hard to read. It is even harder to prove that it works correctly.
Thanks for the advice @vnp. I had added .intValue() as a precautionary measure against the compiler.
â Anirudh Thatipelli
Jul 19 at 15:55
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Time complexity is $O(n)$ indeed. Since every input character must be inspected, this asymptotic is impossible to improve. However, it is possible to improve the constant. See below.
Space complexity is $O(n)$ indeed. However, it is possible to solve the problem is $O(1)$. Consider two iterators, one running left to right, and another running right to left. As long as the first iterator points a consonant, print it. As soon as it hits a vowel, scan the second iterator (right to left) until it finds a vowel, and print that vowel. Notice that this approach also doesn't need
valuesandkeys- and hence doesn't need to build them. This is how time complexity is also improved.The line
sb.replace(num.intValue(),num.intValue() + 1,String.valueOf(values.get(count)));is very hard to read. It is even harder to prove that it works correctly.
Thanks for the advice @vnp. I had added .intValue() as a precautionary measure against the compiler.
â Anirudh Thatipelli
Jul 19 at 15:55
add a comment |Â
up vote
2
down vote
Time complexity is $O(n)$ indeed. Since every input character must be inspected, this asymptotic is impossible to improve. However, it is possible to improve the constant. See below.
Space complexity is $O(n)$ indeed. However, it is possible to solve the problem is $O(1)$. Consider two iterators, one running left to right, and another running right to left. As long as the first iterator points a consonant, print it. As soon as it hits a vowel, scan the second iterator (right to left) until it finds a vowel, and print that vowel. Notice that this approach also doesn't need
valuesandkeys- and hence doesn't need to build them. This is how time complexity is also improved.The line
sb.replace(num.intValue(),num.intValue() + 1,String.valueOf(values.get(count)));is very hard to read. It is even harder to prove that it works correctly.
Thanks for the advice @vnp. I had added .intValue() as a precautionary measure against the compiler.
â Anirudh Thatipelli
Jul 19 at 15:55
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Time complexity is $O(n)$ indeed. Since every input character must be inspected, this asymptotic is impossible to improve. However, it is possible to improve the constant. See below.
Space complexity is $O(n)$ indeed. However, it is possible to solve the problem is $O(1)$. Consider two iterators, one running left to right, and another running right to left. As long as the first iterator points a consonant, print it. As soon as it hits a vowel, scan the second iterator (right to left) until it finds a vowel, and print that vowel. Notice that this approach also doesn't need
valuesandkeys- and hence doesn't need to build them. This is how time complexity is also improved.The line
sb.replace(num.intValue(),num.intValue() + 1,String.valueOf(values.get(count)));is very hard to read. It is even harder to prove that it works correctly.
Time complexity is $O(n)$ indeed. Since every input character must be inspected, this asymptotic is impossible to improve. However, it is possible to improve the constant. See below.
Space complexity is $O(n)$ indeed. However, it is possible to solve the problem is $O(1)$. Consider two iterators, one running left to right, and another running right to left. As long as the first iterator points a consonant, print it. As soon as it hits a vowel, scan the second iterator (right to left) until it finds a vowel, and print that vowel. Notice that this approach also doesn't need
valuesandkeys- and hence doesn't need to build them. This is how time complexity is also improved.The line
sb.replace(num.intValue(),num.intValue() + 1,String.valueOf(values.get(count)));is very hard to read. It is even harder to prove that it works correctly.
answered Jul 18 at 19:41
vnp
36.3k12890
36.3k12890
Thanks for the advice @vnp. I had added .intValue() as a precautionary measure against the compiler.
â Anirudh Thatipelli
Jul 19 at 15:55
add a comment |Â
Thanks for the advice @vnp. I had added .intValue() as a precautionary measure against the compiler.
â Anirudh Thatipelli
Jul 19 at 15:55
Thanks for the advice @vnp. I had added .intValue() as a precautionary measure against the compiler.
â Anirudh Thatipelli
Jul 19 at 15:55
Thanks for the advice @vnp. I had added .intValue() as a precautionary measure against the compiler.
â Anirudh Thatipelli
Jul 19 at 15:55
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f199769%2freversing-the-vowels-in-a-string%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password