Skip to content

Commit ea22e39

Browse files
committed
0482-license-key-formatting
1 parent 48c5707 commit ea22e39

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/// 482. License Key Formatting
2+
/// You are given a license key represented as a string S which consists only alphanumeric
3+
/// character and dashes. The string is separated into N+1 groups by N dashes.
4+
/// Given a number K, we would want to reformat the strings such that each group contains exactly
5+
/// K characters, except for the first group which could be shorter than K, but still must contain
6+
/// at least one character. Furthermore, there must be a dash inserted between two groups and all
7+
/// lowercase letters should be converted to uppercase.
8+
/// Given a non-empty string S and a number K, format the string according to the rules
9+
/// described above.
10+
11+
import XCTest
12+
13+
/// Approach: String
14+
func licenseKeyFormatting(_ s: String, _ k: Int) -> String {
15+
var s = Array(s.uppercased())
16+
var result: [Character] = []
17+
for i in stride(from: s.count - 1, through: 0, by: -1) {
18+
guard s[i] != "-" else { continue }
19+
if result.count % (k + 1) == k {
20+
result.append("-")
21+
}
22+
result.append(s[i])
23+
}
24+
return String(result.reversed())
25+
}
26+
27+
28+
class Tests: XCTestCase {
29+
func testExample() {
30+
let s = "5F3Z-2e-9-w"
31+
let k = 4
32+
XCTAssertEqual(licenseKeyFormatting(s, k), "5F3Z-2E9W")
33+
}
34+
}
35+
36+
Tests.defaultTestSuite.run()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,6 @@ You can visit the pages below to search problems by company tags. Then come back
5252
31 | [Next Permutation](https://leetcode.com/problems/next-permutation/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0031-next-permutation.playground/Contents.swift) | Two Pointers
5353
32 | [Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0032-longest-valid-parentheses.playground/Contents.swift) | Stack
5454
33 | [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0033-search-in-rotated-sorted-array.playground/Contents.swift) | Binary Search
55+
482 | [License Key Formatting](https://leetcode.com/problems/license-key-formatting/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0482-license-key-formatting.playground/Contents.swift) | String
5556
681 | [Next Closest Time](https://leetcode.com/problems/next-closest-time/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0681-next-closest-time.playground/Contents.swift) | Simulation
5657
683 | [K Empty Slots](https://leetcode.com/problems/k-empty-slots/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0683-k-empty-slots.playground/Contents.swift) | Sliding Window

0 commit comments

Comments
 (0)