π’ preface
π Algorithm problem π |
- π² Punching out an algorithm problem every day is not only a learning process, but also a sharing process π
- π² Tip: the problem-solving programming languages in this column are C# and Java
- π² To maintain a state of learning every day, let's work together to become the great God of algorithm π§!
- π² Today is the 99th day of punching out the force deduction algorithm π!
π Algorithm problem π |
π² Example of original question: goat Latin
Give a sentence S with words separated by spaces. Each word contains only uppercase or lowercase letters.
We want to convert the sentence into "Goat Latin" (a fictional language similar to Pig Latin).
The rules of goat Latin are as follows:
-
If the word begins with a vowel (a, e, i, o, u), add "ma" after the word.
For example, the word "apple" becomes "Apple Ma". -
If the word begins with a consonant letter (i.e. a non vowel letter), remove the first character and put it at the end before adding "ma".
For example, the word "goat" becomes "oatgma". -
According to the index of the word in the sentence, add the same number of letters' a 'at the end of the word as the index, and the index starts from 1.
For example, add "a" after the first word, add "aa" after the second word, and so on.
Returns the sentence after converting S to Latin.
Example 1:
input: "I speak Goat Latin" output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
Example 2:
input: "The quick brown fox jumped over the lazy dog" output: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"
Tips:
- S contains only uppercase and lowercase letters and spaces. There is only one space between words.
- 1 <= S.length <= 150.
π» C# method: traversal
Traverse the array, and then make conditional judgment
If it is a vowel, it is added, and if it is a consonant, it is shifted and reprocessed
code:
public class Solution { public string ToGoatLatin(string S) { string[] arr = S.Split(" "); char[] yuan = new char[] { 'a','e','i','o', 'u' ,'A', 'E', 'I', 'O', 'U' }; StringBuilder str = new StringBuilder(); string res = ""; for (int i = 0; i < arr.Length; i++) { if (res!="") { res += " "; } str.Clear(); str.Append(arr[i]); if (yuan.Contains(str[0])) { str.Append("ma"); } else { str.Append(str[0]); str.Remove(0, 1); str.Append("ma"); } for (int j = 0; j <= i; j++) { str.Append("a"); } res += str.ToString(); } return res; } }
results of enforcement
adopt Execution time: 124 msοΌAt all C# Defeated 100.00% of users in submission Memory consumption: 45.9 MBοΌAt all C# Defeated 43.90% of users in submission
π» Java methods: strings
Train of thought analysis
For each word in the sentence, if it is a vowel letter, it will not change; If it is a consonant, rotate the word (word[1:] + word[:1] in Python and word. Substring (1) + word. In Java) substring(0, 1).
Then, we add "ma" and the desired number of "a" and a space.
code:
class Solution { public String toGoatLatin(String S) { Set<Character> vowel = new HashSet(); for (char c: new char[]{'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}) vowel.add(c); int t = 1; StringBuilder ans = new StringBuilder(); for (String word: S.split(" ")) { char first = word.charAt(0); if (vowel.contains(first)) { ans.append(word); } else { ans.append(word.substring(1)); ans.append(word.substring(0, 1)); } ans.append("ma"); for (int i = 0; i < t; i++) ans.append("a"); t++; ans.append(" "); } ans.deleteCharAt(ans.length() - 1); return ans.toString(); } }
results of enforcement
adopt Execution time: 2 msοΌAt all Java Defeated 87 in submission.98%User Memory consumption: 38.3 MBοΌAt all Java Defeated 94 in submission.50%User
Complexity analysis
Time complexity: O( N^2 ) Space complexity: O(N^2)
π¬ summary
- Today is the ninety ninth day of punching out the force deduction algorithm!
- This paper uses C# and Java programming languages to solve problems
- Some methods are also written by Likou God, and they are also shared while learning. Thanks again to the algorithm bosses
- That's the end of today's algorithm sharing. See you tomorrow!