You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
varlicenseKeyFormatting=function(S,K){
42
+
43
+
letcleanStr=S.toUpperCase().replace(/-/g,'');
44
+
45
+
letlen=cleanStr.length;
46
+
47
+
// let s1 is the length of the first group
48
+
49
+
lets1=((len%K)===0) ? K : (len%K);
50
+
51
+
// now form the first group of the resultant string
52
+
letresultStr=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
Copy file name to clipboardExpand all lines: reverse_integer.js
+4-1Lines changed: 4 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -33,6 +33,7 @@ var reverse = function(x) {
33
33
34
34
// // SOLUTION-2 - Best Performing solution. And also if the Problems asks for not to use any string related methods.
35
35
varreverseBest=function(x){
36
+
36
37
vary=Math.abs(x);
37
38
varresult=0;
38
39
@@ -109,4 +110,6 @@ const reverseNum = x => {
109
110
110
111
console.log(reverseNum(123));// => 321
111
112
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. */
0 commit comments