File tree Expand file tree Collapse file tree 3 files changed +41
-0
lines changed
0482-license-key-formatting.playground Expand file tree Collapse file tree 3 files changed +41
-0
lines changed Original file line number Diff line number Diff line change
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 ( )
Original file line number Diff line number Diff line change
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 >
Original file line number Diff line number Diff line change @@ -52,5 +52,6 @@ You can visit the pages below to search problems by company tags. Then come back
52
52
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
53
53
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
54
54
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
55
56
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
56
57
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
You can’t perform that action at this time.
0 commit comments