File tree 2 files changed +34
-0
lines changed
2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change 204
204
461|[ Hamming Distance] ( ./0461-hamming-distance.js ) |Easy|
205
205
463|[ Island Perimeter] ( ./0463-island-perimeter.js ) |Medium|
206
206
476|[ Number Complement] ( ./0476-number-complement.js ) |Easy|
207
+ 482|[ License Key Formatting] ( ./0482-license-key-formatting.js ) |Easy|
207
208
485|[ Max Consecutive Ones] ( ./0485-max-consecutive-ones.js ) |Easy|
208
209
491|[ Non-decreasing Subsequences] ( ./0491-non-decreasing-subsequences.js ) |Medium|
209
210
492|[ Construct the Rectangle] ( ./0492-construct-the-rectangle.js ) |Easy|
You can’t perform that action at this time.
0 commit comments