[String-easy] 824. Goat Latin change according to certain rules of a given string

1. The title site

https://leetcode.com/problems/goat-latin/

2. Title Description

Here Insert Picture Description

3. subject to the effect

Given a string, Goat Latin converted to the format conversion rule is as follows:

  • If the word begins with a vowel, can be added directly behind the word ma.
  • If the word does not begin with a vowel, the first letter of the first word moved to the back, add ma.
  • Starting from the first word, each word plus a number of suffixes, the first word plus "a", the second word plus "aa", and so on.

4. Description Solving

  • First, the use of the split type String method according to each of the spaces divided word string.

5. AC Code

class Solution {
    public String toGoatLatin(String S) {
        String[] a=S.split(" ");
        String vowels="aeiouAEIOU";
        StringBuilder res=new StringBuilder();
        for(int i=0;i<a.length;i++){
            char[] arr=a[i].toCharArray();
             StringBuilder str=new StringBuilder();
            for(int j=0;j<=i;j++){
               str.append("a"); 
            }
            if(vowels.indexOf(arr[0])!=-1) res.append(a[i]+"ma"+str.toString()+" ");
            else
             res.append(new StringBuilder(a[i]).delete(0,1).toString()+arr[0]+"ma"+str.toString()+" ");  
        }
        return res.toString().trim();
    }
}

Guess you like

Origin blog.csdn.net/xiaojie_570/article/details/93396934