Skip to content

Commit 0b29d4e

Browse files
committed
License Key Formatting
1 parent 994739c commit 0b29d4e

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Source : https://leetcode.com/problems/license-key-formatting/
2+
// Author : Han Zichi
3+
// Date : 2017-01-11
4+
// Runtime: 86 ms
5+
6+
// Your runtime beats 100.00% of javascript submissions
7+
8+
/**
9+
* @param {string} S
10+
* @param {number} K
11+
* @return {string}
12+
*/
13+
var licenseKeyFormatting = function(S, K) {
14+
S = S.toUpperCase().replace(/-/g, '');
15+
16+
let ans = [];
17+
let index = 0;
18+
19+
if (S.length % K) {
20+
let str = S.substr(0, S.length % K);
21+
ans.push(str);
22+
index = S.length % K;
23+
}
24+
25+
for (let i = index, len = S.length; i < len; i += K)
26+
ans.push(S.substr(i, K));
27+
28+
return ans.join('-');
29+
};

0 commit comments

Comments
 (0)