Leetcode : 345. Reverse Vowels of a String | Facebook, Amazon

cloudAndTechnology
1 min readJun 22, 2021

Company : Facebook, Amazon

Question : Given a string s, reverse only all the vowels in the string and return it.

The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in both cases.

Example 1:

Input: s = "hello"
Output: "holle"

Example 2:

Input: s = "leetcode"
Output: "leotcede"

Constraints:

  • 1 <= s.length <= 3 * 105
  • s consist of printable ASCII characters.

Accepted

Solution :

Runtime: 3 ms, faster than 80.73% of Java online submissions for Reverse Vowels of a String.

Memory Usage: 39.8 MB, less than 36.50% of Java online submissions for Reverse Vowels of a String.

Program :class Solution {
public String reverseVowels(String s) {
char ss[]=s.toCharArray();
int i=0;
int j=s.length()-1;
char c=’A’;
while(i<j)
{

while((i<j) && (!(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’)))
i++;



while((i<j) && (!(s.charAt(j)==’a’ || s.charAt(j)==’e’ ||s.charAt(j)==’i’ ||s.charAt(j)==’o’ ||s.charAt(j)==’u’ || s.charAt(j)==’A’ || s.charAt(j)==’E’ || s.charAt(j)==’I’ || s.charAt(j)==’O’ || s.charAt(j)==’U’)))
j — ;

if(i==j) break;
c=ss[i];
ss[i]=ss[j];
ss[j]=c;
i++;
j — ;
}
return String.valueOf(ss);

}
}

--

--