Leet Code: - 345. Reverse Vowels of a String
345. Reverse Vowels of a String
Intuition
using two pointers
Approach
we start convert the string to array than check both from first to last and swap if two character is vowel
Complexity
Time complexity: O(n)
Space complexity: O(n)
Code
class Solution {
public String reverseVowels(String s) {
char ch[] = s.toCharArray();
int i = 0;
int j = ch.length - 1;
while(i < j){
if((ch[i] == 'a' || ch[i] == 'e' || ch[i] == 'i' || ch[i] == 'o' || ch[i] == 'u' || ch[i] == 'A' || ch[i] == 'E' ||
ch[i] == 'I' || ch[i] == 'O' || ch[i] == 'U') && (ch[j] == 'a' || ch[j] == 'e' || ch[j] == 'i' || ch[j] == 'o' ||
ch[j] == 'u' || ch[j] == 'A' || ch[j] == 'E' || ch[j] == 'I' || ch[j] == 'O' || ch[j] == 'U')){
char temp = ch[i];
ch[i] = ch[j];
ch[j] = temp;
i++;
j--;
}else if(ch[i] == 'a' || ch[i] == 'e' || ch[i] == 'i' || ch[i] == 'o' || ch[i] == 'u' ||
ch[i] == 'A' || ch[i] == 'E' || ch[i] == 'I' || ch[i] == 'O' || ch[i] == 'U'){
j--;
}else if(ch[j] == 'a' || ch[j] == 'e' || ch[j] == 'i' || ch[j] == 'o' || ch[j] == 'u' ||
ch[j] == 'A' || ch[j] == 'E' || ch[j] == 'I' || ch[j] == 'O' || ch[j] == 'U'){
i++;
}else{
i++;
j--;
}
}
String str = new String(ch);
return str;
}
}
Comments
Post a Comment