824. Goat Latin

824. Goat Latin

class Solution {
public String toGoatLatin(String S) {
		StringBuilder out = new StringBuilder();

		String[] words = S.split(" ");

		for (int i = 0; i < words.length; i++) {

			if (words[i].charAt(0) == 'a' || words[i].charAt(0) == 'e' || words[i].charAt(0) == 'i'
					|| words[i].charAt(0) == 'o' || words[i].charAt(0) == 'u' || words[i].charAt(0) == 'A'
					|| words[i].charAt(0) == 'E' || words[i].charAt(0) == 'I' || words[i].charAt(0) == 'O'
					|| words[i].charAt(0) == 'U') {
				words[i] += "ma";
			} else {
				words[i] = words[i].substring(1) + words[i].charAt(0)+ "ma";
			}

			for (int j = 1; j <= i + 1; j++)
				words[i] += "a";
			if (i == words.length - 1)// end of sentence
				out.append(words[i]);
			else
				out.append(words[i] + " ");
		}

		return out.toString();
	}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s