Skip to content

Commit

Permalink
0482-license-key-formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
bofeizhu committed Aug 19, 2018
1 parent 48c5707 commit ea22e39
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
36 changes: 36 additions & 0 deletions 0482-license-key-formatting.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/// 482. License Key Formatting
/// You are given a license key represented as a string S which consists only alphanumeric
/// character and dashes. The string is separated into N+1 groups by N dashes.
/// Given a number K, we would want to reformat the strings such that each group contains exactly
/// K characters, except for the first group which could be shorter than K, but still must contain
/// at least one character. Furthermore, there must be a dash inserted between two groups and all
/// lowercase letters should be converted to uppercase.
/// Given a non-empty string S and a number K, format the string according to the rules
/// described above.

import XCTest

/// Approach: String
func licenseKeyFormatting(_ s: String, _ k: Int) -> String {
var s = Array(s.uppercased())
var result: [Character] = []
for i in stride(from: s.count - 1, through: 0, by: -1) {
guard s[i] != "-" else { continue }
if result.count % (k + 1) == k {
result.append("-")
}
result.append(s[i])
}
return String(result.reversed())
}


class Tests: XCTestCase {
func testExample() {
let s = "5F3Z-2e-9-w"
let k = 4
XCTAssertEqual(licenseKeyFormatting(s, k), "5F3Z-2E9W")
}
}

Tests.defaultTestSuite.run()
4 changes: 4 additions & 0 deletions 0482-license-key-formatting.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
<timeline fileName='timeline.xctimeline'/>
</playground>
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@ You can visit the pages below to search problems by company tags. Then come back
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
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
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
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
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
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 comments on commit ea22e39

Please sign in to comment.