-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
824 Goat Latin.swift
37 lines (31 loc) 路 1.08 KB
/
824 Goat Latin.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//
// 824 Goat Latin.swift
// LeetCode-Solutions
//
// Created by Aleksandar Dinic on 19/08/2020.
// Copyright 漏 2020 Aleksandar Dinic. All rights reserved.
//
import Foundation
/// Source: https://leetcode.com/problems/goat-latin/
class Solution {
/// Converts the sentence to "Goat Latin".
///
/// - Parameter S: The sentence.
/// - Returns: The sentence representing the conversion to Goat Latin.
///
/// - Complexity:
/// - time: O(n), where n is the length of the sentence.
/// - space: O(m), where m is the length of the sentence on Goat Latin.
func toGoatLatin(_ S: String) -> String {
let vowels = Set(arrayLiteral: "a","e","i","o","u")
var ans = S.split(separator: " ")
for i in 0..<ans.count {
if let first = ans[i].first?.lowercased(), !vowels.contains(first) {
ans[i].append(ans[i].removeFirst())
}
ans[i].append(contentsOf: "ma")
ans[i].append(contentsOf: String(repeating: "a", count: i+1))
}
return ans.joined(separator: " ")
}
}