|
1 | 1 | // Alternative arrange the two given strings in one string in O(n) time complexity. |
2 | 2 |
|
3 | | -// Problem Source & Explanation: https://www.geeksforgeeks.org/alternatively-merge-two-strings-in-java/ |
| 3 | +// Problem Source & Explanation: https://www.geeksforgeeks.org/alternatively-merge-two-strings-in-java/ |
4 | 4 |
|
5 | 5 | /** |
6 | 6 | * Alternative arrange the two given strings in one string in O(n) time complexity. |
|
9 | 9 | * @returns `String` return one alternative arrange string. |
10 | 10 | */ |
11 | 11 | const AlternativeStringArrange = (str1, str2) => { |
12 | | - |
13 | 12 | // firstly, check that both inputs are strings. |
14 | 13 | if (typeof str1 !== 'string' || typeof str2 !== 'string') { |
15 | 14 | return 'Not string(s)' |
16 | 15 | } |
17 | 16 |
|
18 | 17 | // output string vlaue. |
19 | | - let out_str = ""; |
| 18 | + let outStr = '' |
20 | 19 |
|
21 | 20 | // get first string length. |
22 | | - const firstStringLength = str1.length; |
| 21 | + const firstStringLength = str1.length |
23 | 22 | // get second string length. |
24 | | - const secondStringLength = str2.length; |
| 23 | + const secondStringLength = str2.length |
25 | 24 | // absolute length for oparetion. |
26 | | - let absLenght = firstStringLength > secondStringLength ? firstStringLength : secondStringLength; |
| 25 | + const absLenght = firstStringLength > secondStringLength ? firstStringLength : secondStringLength |
27 | 26 |
|
28 | 27 | // Iterate the character count until the absolute count is reached. |
29 | 28 | for (let charCount = 0; charCount < absLenght; charCount++) { |
30 | 29 | // If firstStringLength is lesser than the charCount it means they are able to re-arange. |
31 | 30 | if (charCount < firstStringLength) { |
32 | | - out_str += str1[charCount]; |
| 31 | + outStr += str1[charCount] |
33 | 32 | } |
34 | 33 |
|
35 | 34 | // If secondStringLength is lesser than the charCount it means they are able to re-arange. |
36 | 35 | if (charCount < secondStringLength) { |
37 | | - out_str += str2[charCount]; |
| 36 | + outStr += str2[charCount] |
38 | 37 | } |
39 | 38 | } |
40 | 39 |
|
41 | 40 | // return the output string. |
42 | | - return out_str; |
| 41 | + return outStr |
43 | 42 | } |
44 | 43 |
|
45 | | -module.exports = AlternativeStringArrange; |
| 44 | +module.exports = AlternativeStringArrange |
0 commit comments