|
| 1 | +/*: |
| 2 | +# 824. Goat Latin [Easy] |
| 3 | + https://leetcode.com/problems/goat-latin/ |
| 4 | + |
| 5 | + --- |
| 6 | + |
| 7 | +### Problem Statement: |
| 8 | + |
| 9 | + A sentence `S` is given, composed of words separated by spaces. Each word consists of lowercase and uppercase letters only. |
| 10 | + |
| 11 | + We would like to convert the sentence to "Goat Latin" (a made-up language similar to Pig Latin.) |
| 12 | + |
| 13 | + The rules of Goat Latin are as follows: |
| 14 | + |
| 15 | + + 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'. |
| 16 | + |
| 17 | + + 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"`. |
| 18 | + |
| 19 | + + 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. |
| 20 | + |
| 21 | + Return the final sentence representing the conversion from `S` to Goat Latin. |
| 22 | + |
| 23 | + |
| 24 | +### Example: |
| 25 | + |
| 26 | + ``` |
| 27 | + Input: "I speak Goat Latin" |
| 28 | + Output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa" |
| 29 | + |
| 30 | + Input: "The quick brown fox jumped over the lazy dog" |
| 31 | + Output: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa" |
| 32 | + |
| 33 | + ``` |
| 34 | + |
| 35 | + ### Constraints: |
| 36 | + + S contains only uppercase, lowercase and spaces. Exactly one space between each word. |
| 37 | + + 1 <= S.length <= 150. |
| 38 | + |
| 39 | + */ |
| 40 | + |
| 41 | +import UIKit |
| 42 | + |
| 43 | +extension Character { |
| 44 | + var isVowel: Bool { |
| 45 | + switch self { |
| 46 | + case "a", "e", "i", "o", "u", "A", "E", "I", "O", "U": |
| 47 | + return true |
| 48 | + default: |
| 49 | + return false |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// 99 / 99 test cases passed. |
| 55 | +// Status: Accepted |
| 56 | +// Runtime: 16 ms |
| 57 | +// Memory Usage: 21.7 MB |
| 58 | + |
| 59 | +class Solution { |
| 60 | + |
| 61 | + func toGoatLatin(_ S: String) -> String { |
| 62 | + |
| 63 | + let words = S.components(separatedBy: " ") |
| 64 | + var resultString = "" |
| 65 | + var extraWord = "" |
| 66 | + |
| 67 | + for word in words { |
| 68 | + |
| 69 | + extraWord += "a" |
| 70 | + |
| 71 | + var convertedWord = word |
| 72 | + if !(word.first!.isVowel) { |
| 73 | + let first = convertedWord.removeFirst() |
| 74 | + convertedWord += String(first) |
| 75 | + } |
| 76 | + |
| 77 | + resultString += "\(convertedWord)ma\(extraWord) " |
| 78 | + } |
| 79 | + |
| 80 | + resultString.removeLast() |
| 81 | + return resultString |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +let sol = Solution() |
| 86 | +sol.toGoatLatin("I speak Goat Latin") // Imaa peaksmaaa oatGmaaaa atinLmaaaaa |
| 87 | + |
| 88 | + |
| 89 | +// Output: |
| 90 | +// heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa |
| 91 | +sol.toGoatLatin("The quick brown fox jumped over the lazy dog") |
0 commit comments