• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode824.山羊拉丁文|GoatLatin

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10588657.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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

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

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. 

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"

Notes:

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

给定一个由空格分割单词的句子 S。每个单词只包含大写或小写字母。

我们要将句子转换为 “Goat Latin”(一种类似于 猪拉丁文 - Pig Latin 的虚构语言)。

山羊拉丁文的规则如下:

    • 如果单词以元音开头(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 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap