Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit fb71211

Browse files
committed
Add solution #482
1 parent 4eb54a3 commit fb71211

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

0482-license-key-formatting.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 482. License Key Formatting
3+
* https://leetcode.com/problems/license-key-formatting/
4+
* Difficulty: Easy
5+
*
6+
* You are given a license key represented as a string s that consists of only alphanumeric
7+
* characters and dashes. The string is separated into n + 1 groups by n dashes. You are
8+
* also given an integer k.
9+
*
10+
* We want to reformat the string s such that each group contains exactly k characters,
11+
* except for the first group, which could be shorter than k but still must contain at
12+
* least one character. Furthermore, there must be a dash inserted between two groups,
13+
* and you should convert all lowercase letters to uppercase.
14+
*
15+
* Return the reformatted license key.
16+
*/
17+
18+
/**
19+
* @param {string} s
20+
* @param {number} k
21+
* @return {string}
22+
*/
23+
var licenseKeyFormatting = function(s, k) {
24+
const result = [];
25+
s = s.toUpperCase().replace(/\-+/g, '');
26+
if (s.length % k) {
27+
result.push(s.substring(0, s.length % k));
28+
}
29+
for (let i = s.length % k; i < s.length; i += k) {
30+
result.push(s.substring(i, i + k));
31+
}
32+
return result.join('-');
33+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@
204204
461|[Hamming Distance](./0461-hamming-distance.js)|Easy|
205205
463|[Island Perimeter](./0463-island-perimeter.js)|Medium|
206206
476|[Number Complement](./0476-number-complement.js)|Easy|
207+
482|[License Key Formatting](./0482-license-key-formatting.js)|Easy|
207208
485|[Max Consecutive Ones](./0485-max-consecutive-ones.js)|Easy|
208209
491|[Non-decreasing Subsequences](./0491-non-decreasing-subsequences.js)|Medium|
209210
492|[Construct the Rectangle](./0492-construct-the-rectangle.js)|Easy|

0 commit comments

Comments
 (0)