[Swift]LeetCode824. 山羊拉丁文 | Goat Latin

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-wekbplhm-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and uppercase letters only.git

We would like to convert the sentence to "Goat Latin" (a made-up language similar to Pig Latin.)github

The rules of Goat Latin are as follows:微信

  • If a word begins with a vowel (a, e, i, o, or u), append "ma" to the end of the word.
    For example, the word 'apple' becomes 'applema'.
     
  • If a word begins with a consonant (i.e. not a vowel), remove the first letter and append it to the end, then add "ma".
    For example, the word "goat" becomes "oatgma".
     
  • Add one letter 'a' to the end of each word per its word index in the sentence, starting with 1.
    For example, the first word gets "a"added to the end, the second word gets "aa" added to the end and so on.

Return the final sentence representing the conversion from S to Goat Latin. app

Example 1:ui

Input: "I speak Goat Latin"
Output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"

Example 2:spa

Input: "The quick brown fox jumped over the lazy dog"
Output: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"

Notes:code

  • S contains only uppercase, lowercase and spaces. Exactly one space between each word.
  • 1 <= S.length <= 150.

給定一個由空格分割單詞的句子 S。每一個單詞只包含大寫或小寫字母。component

咱們要將句子轉換爲 「Goat Latin」(一種相似於 豬拉丁文 - Pig Latin 的虛構語言)。htm

山羊拉丁文的規則以下:

    • 若是單詞以元音開頭(a, e, i, o, u),在單詞後添加"ma"
      例如,單詞"apple"變爲"applema"

 

    • 若是單詞以輔音字母開頭(即非元音字母),移除第一個字符並將它放到末尾,以後再添加"ma"
      例如,單詞"goat"變爲"oatgma"
    • 根據單詞在句子中的索引,在單詞最後添加與索引相同數量的字母'a',索引從1開始。

例如,在第一個單詞後添加"a",在第二個單詞後添加"aa",以此類推。

返回將 S 轉換爲山羊拉丁文後的句子。

示例 1:

輸入: "I speak Goat Latin"
輸出: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"

示例 2:

輸入: "The quick brown fox jumped over the lazy dog"
輸出: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"

說明:

  • S 中僅包含大小寫字母和空格。單詞間有且僅有一個空格。
  • 1 <= S.length <= 150

Runtime: 12 ms
Memory Usage: 20.5 MB
 1 import XCTest  2 class Solution {  3     func toGoatLatin(_ S: String) -> String {  4         let vowels = ["a", "e", "i", "o", "u"]  5         var result = ""
 6         S.split(separator: " ").enumerated().forEach { i, word in
 7         guard let firstChar = word.first.map(String.init) else { return }  8         if !vowels.contains(firstChar.lowercased()) {  9             result.append(word.dropFirst() + firstChar + "ma") 10         } else { 11             result.append(word + "ma") 12  } 13         result += String(repeating: "a", count: i + 1) + " "
14  } 15     return String(result.dropLast()) 16  } 17 }

12ms

 1 class Solution {  2     func toGoatLatin(_ S: String) -> String {  3         var result: String = ""
 4         var toAppend = ""
 5         let ma = "ma"
 6         var a = ""
 7         
 8         enum Status {  9             case blank 10             case character 11  } 12         var status = Status.blank 13         
14         for char in S { 15             switch status { 16             case .blank: 17                 switch char { 18                 case " ": 19                     break
20                 case "a", "e", "i", "o", "u", "A", "E", "I", "O", "U": 21                     result.append(char) 22                     toAppend = ""
23                     a += "a"
24                     status = .character 25                 default: 26                     toAppend = String(char) 27                     a += "a"
28                     status = .character 29  } 30             case .character: 31                 switch char { 32                 case " ": 33  result.append(toAppend) 34  result.append(ma) 35  result.append(a) 36                     result.append(char) 37                     status = .blank 38                 default: 39                     result.append(char) 40  } 41  } 42  } 43  result.append(toAppend) 44  result.append(ma) 45  result.append(a) 46         return result 47  } 48 }

16ms

 1 class Solution {  2     var vowels : Set<Character> = ["a","e","i","o","u","A","E","I","O","U"]  3     func toGoatLatin(_ S: String) -> String {  4         var result = ""
 5         let subStrs = S.split(separator: " ")  6         var  suffix  = "a"
 7         for item in subStrs {  8             var temp = item  9             if !vowels.contains(temp.first!) { 10                 let char = temp.removeFirst() 11                 temp.append(char) 12  } 13             result += (temp + "ma" + suffix + " ") 14             suffix += "a"
15  } 16  result.removeLast() 17         return result 18  } 19 }

20ms

 1 class Solution {  2     func toGoatLatin(_ S: String) -> String {  3   let setVowels = CharacterSet(charactersIn: "aeiou")  4         var arrayWords = [String]()  5         
 6         for (index,word) in S.components(separatedBy: " ").enumerated(){  7             
 8             var newWord = word  9             if String(word.first!).lowercased().rangeOfCharacter(from: setVowels) != nil{ 10                 newWord.append("ma") 11  } 12             else{ 13  newWord.append(newWord.removeFirst()) 14                 newWord.append("ma") 15  } 16             
17             for _ in 0...index{ 18                 newWord.append("a") 19  } 20  arrayWords.append(newWord) 21  } 22         
23         return arrayWords.joined(separator: " ") 24  } 25 }

24ms

 1 class Solution {  2     func toGoatLatin(_ S: String) -> String {  3         let y = ["a","A","e","E","i","I","o","O","u","U"]  4         let a = S.components(separatedBy: " ")  5         var b = "a"
 6         var c:[String] = []  7         var d:[String] = []  8         for var i in a {  9             if y.contains((i[i.startIndex]).description){ 10                 i+="ma"
11             }else{ 12  i.append(i[i.startIndex]) 13  i.remove(at: i.startIndex) 14                 i+="ma"
15  } 16  c.append(i) 17  } 18         for var i in c { 19  i.append(b) 20  d.append(i) 21             b+="a"
22  } 23         
24         return d.joined(separator: " ") 25  } 26 }

28ms

 1 class Solution {  2     func toGoatLatin(_ S: String) -> String {  3          var temp = S.split(separator: " ")  4         var goatLatin = ""
 5          for (i,v) in temp.enumerated(){  6              var result = ""
 7             if  ["a","e", "i", "o", "u","A","E","I","O","U"].contains(v.first) {  8                 result =  String(v) + "ma"
 9  } 10             else { 11                 result = String(v) + String(v.first!) 12  result.remove(at:result.startIndex) 13                 result = result + "ma"
14  } 15              for index in  0 ..< i+1 { 16             result = result + "a"
17  } 18               goatLatin = goatLatin + result + " "
19  } 20         return goatLatin.trimmingCharacters(in: .whitespaces) 21  } 22 }

28ms

 1 class Solution {  2 func toGoatLatin(_ S: String) -> String {  3     let chaArr = "aeiouAEIOU"
 4     var tmpArr = [String]()  5     let strArr = S.components(separatedBy: " ")  6     for (index,item) in strArr.enumerated() {  7         var str = item  8         if chaArr.contains(item.first!) && item.count != 1 {  9             str = str + "ma"
10         } else { 11             str.append(item.first!) 12  str.removeFirst() 13             str.append("ma") 14  } 15         var tmpIndex = index + 1
16         while(tmpIndex != 0) { 17             str.append("a") 18             tmpIndex -= 1
19  } 20  tmpArr.append(str) 21  } 22     return tmpArr.joined(separator: " ") 23  } 24 }

32ms

 1 class Solution {  2     func toGoatLatin(_ S: String) -> String {  3         let vowelSet = Set<Character>(["A", "E", "I", "O", "U", "a", "e", "i", "o", "u"])  4         var charsArr = S.split(separator: " ").map { Array($0) }  5         let ma: [Character] = ["m", "a"]  6         let a: Character = "a"
 7 
 8         for (i, var chars) in charsArr.enumerated() {  9             if !vowelSet.contains(chars[0]) { 10                 let firstChar = chars.remove(at: 0) 11  chars.append(firstChar) 12  } 13  chars.append(contentsOf: ma) 14             chars.append(contentsOf: Array(repeating: a, count: i + 1)) 15 
16             charsArr[i] = chars 17  } 18 
19         return charsArr.map { String($0) }.joined(separator: " ") 20  } 21 }

44ms

 1 class Solution {  2     func toGoatLatin(_ S: String) -> String {  3         var newString = ""
 4         var posfix = ""
 5         let maString = "ma"
 6         let vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]  7         let words = S.components(separatedBy: CharacterSet.whitespaces)  8         
 9         for i in 0..<words.count { 10             posfix += "a"
11             let word = words[i] 12             let prefix = String(word.first!) 13             if vowels.contains(prefix) { 14                 newString += word + maString + posfix 15             } else { 16                 newString += word[word.index(after: word.startIndex)..<word.endIndex] + prefix + maString + posfix 17  } 18             
19             if i < words.count - 1 { 20                 newString += " "
21  } 22  } 23         
24         return newString 25  } 26 }

48ms

 1 class Solution {  2     func toGoatLatin(_ S: String) -> String {  3                 var str = ""
 4         let arr:[Character] = ["a","o","i","e","u"]  5         let sarr = S.components(separatedBy: " ")  6         
 7         for j in 0..<sarr.count {  8             let x = sarr[j]  9             var tolast = ""
10             var a = ""
11             for _ in 0..<j+1{ 12                 a += "a"
13  } 14             
15             var index = 0
16             for y in x{ 17                 if index == 0{ 18                     let z  = Character(String(y).lowercased()) 19                     if !arr.contains(z){ 20                         tolast = String(y) 21                     }else{ 22                         str += String(y) 23  } 24                 }else{ 25                     str += String(y) 26  } 27                 index += 1
28  } 29             
30             if tolast != ""{ 31                 str += tolast 32  } 33             
34             str += "ma"
35             str += a 36             if j < sarr.count-1{ 37                 str += " "
38  } 39  } 40         return str 41  } 42 }
相關文章
相關標籤/搜索