-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.swift
54 lines (48 loc) · 1.25 KB
/
main.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//
// main.swift
// SwiftAlgorithm
//
// Created by Lee Myeonghwan on 2023/04/24.
//
import Foundation
func solution(_ s:String) -> Int {
let numberDict: [String: String] = [
"zero": "0",
"one": "1",
"two": "2",
"three": "3",
"four": "4",
"five": "5",
"six": "6",
"seven": "7",
"eight": "8",
"nine": "9",
]
var components: [String] = []
var currentComponent = ""
let strNumbers = numberDict.keys.map{ String($0) }
let numbers = numberDict.values.map{ String($0) }
let stringToSplit = Array(s)
for c in stringToSplit {
if numbers.contains(String(c)) {
components.append(String(c))
} else {
currentComponent.append(c)
}
if strNumbers.contains(currentComponent) {
components.append(numberDict[currentComponent]!)
currentComponent = ""
}
}
return Int(components.joined())!
}
/* other's
func solution(_ s:String) -> Int {
let arr = ["zero","one","two","three","four","five","six","seven","eight","nine"]
var str = s
for i in 0..<arr.count {
str = str.replacingOccurrences(of: arr[i], with: String(i))
}
return Int(str)!
}
*/