[345] LeetCode inverted vowels string

Title:
Write a function as an input string, the string inversion of vowels.

Example 1:

Input: "hello"
Output: "holle"
Example 2:

Input: "leetcode"
Output: "leotcede"
Description:
vowel does not contain the letter "y".

public class LeetCode345 {

    public String reverseVowels(String s) {
        if(s == null)
            return null;
        int l = 0;
        int r = s.length()-1;
        char[] str = s.toCharArray();
        while(l<r){
            if(isVowel(s.charAt(l)) && isVowel(s.charAt(r))){
                char temp = str[l];
                str[l] = str[r];
                str[r] = temp;
                l++;
                r--;
            }
            if(!isVowel(str[l])){
                l++;
            }
            if(!isVowel(str[r])){
                r--;
            }
        }
        return new String(str);
    }

    boolean isVowel(char c){
        return c=='a' || c == 'e'|| c== 'i' || c== 'o' || c== 'u' ||
                c=='A' || c == 'E'|| c== 'I' || c== 'O' || c== 'U' ;
    }
}
Published 55 original articles · won praise 14 · views 20000 +

Guess you like

Origin blog.csdn.net/qq422243639/article/details/103743816