-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.swift
40 lines (34 loc) · 1.17 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
//
// main.swift
// SwiftAlgorithm
//
// Created by Lee Myeonghwan on 2023/01/20.
//
import Foundation
func solution(_ name:String) -> Int {
let targetArray: [String] = name.map{ String($0) }
var currentAray: [String] = Array(repeating: "A", count: targetArray.count)
var minMove = targetArray.count - 1
var answer = 0
for index in 0..<targetArray.count {
if currentAray[index] != targetArray[index] {
var currentChar = currentAray[index]
var updownCnt = 0
while currentChar != targetArray[index] {
currentChar = String(UnicodeScalar(UnicodeScalar(currentChar)!.value + 1)!)
updownCnt += 1
}
currentAray[index] = currentChar
answer += min(updownCnt, 26 - updownCnt)
}
var endIdx = index + 1
while endIdx < targetArray.count && targetArray[endIdx] == "A" {
endIdx += 1
}
let moveFront = index * 2 + (currentAray.count - endIdx)
let moveBack = (currentAray.count - endIdx) * 2 + index
minMove = min(minMove, moveFront)
minMove = min(minMove, moveBack)
}
return answer + minMove
}