Skip to content

Commit 5ca2dc7

Browse files
committed
license key formatting
1 parent 2699c93 commit 5ca2dc7

30 files changed

+71
-1
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

license-Key-Formatting.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* // https://leetcode.com/problems/license-key-formatting/description/
2+
3+
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.
4+
5+
Rule - 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.
6+
7+
Given a non-empty string S and a number K, format the string according to the rules described above.
8+
9+
Example 1:
10+
Input: S = "5F3Z-2e-9-w", K = 4
11+
12+
Output: "5F3Z-2E9W"
13+
14+
Explanation: The string S has been split into two parts, each part has 4 characters.
15+
Note that the two extra dashes are not needed and can be removed.
16+
Example 2:
17+
Input: S = "2-5g-3-J", K = 2
18+
19+
Output: "2-5G-3J"
20+
21+
Explanation: The string S has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above. */
22+
23+
/* Algo -
24+
25+
A) Replace all the "-" from the given string with empty space, so I am left with a clean alpha-numeric string
26+
27+
B) Looking at the above input example, Length of the first group will have K elements ONLY if len is divisible by K .
28+
29+
C) And if len is NOT divisible then make the length of the first group to be equal to the remainder (of len / K ) - so the rest of the group can each take K letters.
30+
31+
D) Now use substring() to form the first group - by extracting letters from 0-index upto length-of-first-group - 1. Meaning if ength-of-first-group is 4, I have to extract upto 3. And substring() by definition works like that. It does not include the indexEnd (its second argument )
32+
33+
str.substring(indexStart[, indexEnd]) - substring() extracts characters from indexStart up to but not including indexEnd
34+
35+
indexStart - The index of the first character to include in the returned substring.
36+
indexEnd - Optional. The index of the first character to exclude from the returned substring.
37+
Return value - A new string containing the specified part of the given string.
38+
39+
*/
40+
41+
var licenseKeyFormatting = function(S, K) {
42+
43+
let cleanStr = S.toUpperCase().replace(/-/g, '');
44+
45+
let len = cleanStr.length;
46+
47+
// let s1 is the length of the first group
48+
49+
let s1 = (( len % K ) === 0 ) ? K : (len % K);
50+
51+
// now form the first group of the resultant string
52+
let resultStr = cleanStr.substring(0, s1);
53+
54+
// Now that, I have the first group, the next group will be >> first-group + "-" + K elements of the string
55+
// Also as a final reconciliation after I place all the K number of elements in each group the total length should be equal to cleanStr length
56+
// i.e. s1 + K + K ... === len . Till this is not the case, I have to keep placing each K elements to each group
57+
58+
while (len > s1 ) {
59+
resultStr += "-" + cleanStr.substring(s1, s1 + K)
60+
s1 += K
61+
}
62+
return resultStr;
63+
};
64+
65+
console.log(licenseKeyFormatting("5F3Z-2e-9-w", 4)) // => 5F3Z-2E9W
66+
67+
console.log(licenseKeyFormatting("2-5g-3-J", 2)) // => "2-5G-3J"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

solutions/reverse_integer.js renamed to reverse_integer.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var reverse = function(x) {
3333

3434
// // SOLUTION-2 - Best Performing solution. And also if the Problems asks for not to use any string related methods.
3535
var reverseBest = function(x) {
36+
3637
var y = Math.abs(x);
3738
var result = 0;
3839

@@ -109,4 +110,6 @@ const reverseNum = x => {
109110

110111
console.log(reverseNum(123)); // => 321
111112
console.log(reverseNum(-123)); // => -321
112-
console.log(reverseNum( 1534236469 )) // => 0
113+
console.log(reverseNum( 1534236469 )) // => 0
114+
115+
/* split() is a string’s prototype method that converts a string to an array. Providing an empty string as an argument means split by each character. So the resulting array would have each character as an element of the array. */
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)