-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeet482.swift
More file actions
61 lines (56 loc) · 1.59 KB
/
Leet482.swift
File metadata and controls
61 lines (56 loc) · 1.59 KB
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
55
56
57
58
59
60
61
class Solution {
// 내가 푼 풀이
func licenseKeyFormatting_1(_ s: String, _ k: Int) -> String {
var s = s.replacingOccurrences(of: "-", with: "").uppercased()
var answer = ""
var count = 0
while !s.isEmpty {
if count < k {
answer = "\(s.removeLast())" + answer
count += 1
} else {
answer = "-" + answer
count = 0
}
}
return answer
}
// 참고 한 풀이 1
func licenseKeyFormatting_2(_ s: String, _ k: Int) -> String {
var answer = ""
let s = Array(s.uppercased()).filter { $0 != "-" }
let firstSetCount = s.count % k
for i in 0..<firstSetCount {
answer.append(s[i])
}
for i in firstSetCount..<s.count {
if (i-firstSetCount) % k == 0, i != 0 {
answer.append("-")
}
answer.append(s[i])
}
return answer
}
// 참고 한 풀이 2
func licenseKeyFormatting_3(_ s: String, _ k: Int) -> String {
var answer = String()
var index = s.count-1
var count = 0
let s = Array(s.uppercased())
while index >= 0 {
if s[index] == "-" {
index -= 1
continue
}
if count == k {
answer.append("-")
count = 0
} else {
answer.append(s[index])
count += 1
index -= 1
}
}
return String(answer.reversed())
}
}