i was solving a problem of Reverse Vowels of a String. Question states that 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 lower and upper cases, more than once.
Example 1:
Input: s = "hello"
Output: "holle"
This is the actual Java code without lambda function:
class Solution {
public String reverseVowels(String s) {
int i=0,j=s.length()-1,n=s.length()-1;
HashSet<Character> hm=new HashSet<>(Arrays.asList('a','A','e','E','i','I','o','O','u','U'));
char[] ch=s.toCharArray();
while(i<j){
//Character first=null,second=null;
while(i<=n && !hm.contains(ch[i])){
i++;
}
while(j>=0 && !hm.contains(ch[j])){
j--;
}
if(i<j){
char temp=ch[i];
ch[i++]=ch[j];
ch[j--]=temp;
}
}
return new String(ch);
}
}
I tried to convert into lambda8 stream but getting can't find symbol hm, stream
class Solution {
public String reverseVowels(String s) {
int i=0,j=s.length()-1,n=s.length()-1;
Stream<Character> hm=Stream.of('a','A','e','E','i','I','o','O','u','U');
char[] ch=s.toCharArray();
while(i<j){
//Character first=null,second=null;
while(i<=n && (hm.stream().anyMatch(element->element==ch[i]))==false){
i++;
}
while(j>=0 && (hm.stream().anyMatch(element->element==ch[i]))==false){
j--;
}
if(i<j){
char temp=ch[i];
ch[i++]=ch[j];
ch[j--]=temp;
}
}
return new String(ch);
}
}