From 6893c4f310d503a7d6487ee5e53b8c6c96822971 Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Wed, 18 Aug 2021 17:44:02 +0530 Subject: [PATCH 1/5] add AlternativeStringArrange method in string category --- String/AlternativeStringArrange.js | 42 ++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 String/AlternativeStringArrange.js diff --git a/String/AlternativeStringArrange.js b/String/AlternativeStringArrange.js new file mode 100644 index 000000000..cf3d41868 --- /dev/null +++ b/String/AlternativeStringArrange.js @@ -0,0 +1,42 @@ +// Alternative arrange the two given strings in one string in O(n) time complexity. +/** + * Alternative arrange the two given strings in one string in O(n) time complexity. + * @param {String} str1 first input string + * @param {String} str2 second input string + * @returns `String` return one alternative arrange string. + */ + const AlternativeStringArrange = (str1, str2) => { + + // firstly, check that both inputs are strings. + if (typeof str1 !== 'string' || typeof str2 !== 'string') { + return 'Not string(s)' + } + + // output string vlaue. + let out_str = ""; + + // get first string length. + const firstStringLength = str1.length; + // get second string length. + const secondStringLength = str2.length; + // absolute length for oparetion. + let absLenght = firstStringLength > secondStringLength ? firstStringLength : secondStringLength; + + // Iterate the character count until the absolute count is reached. + for(let charCount = 0; charCount < absLenght; charCount++){ + // If firstStringLength is lesser than the charCount it means they are able to re-arange. + if(charCount < firstStringLength){ + out_str += str1[charCount]; + } + + // If secondStringLength is lesser than the charCount it means they are able to re-arange. + if(charCount < secondStringLength){ + out_str += str2[charCount]; + } + } + + // return the output string. + return out_str; +} + +module.exports = AlternativeStringArrange; \ No newline at end of file From 29fee5fbcee5842992a37ba2ff1ee7536df1d57d Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Wed, 18 Aug 2021 17:45:03 +0530 Subject: [PATCH 2/5] add new line at the end of the file --- String/AlternativeStringArrange.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/String/AlternativeStringArrange.js b/String/AlternativeStringArrange.js index cf3d41868..a484c6cf3 100644 --- a/String/AlternativeStringArrange.js +++ b/String/AlternativeStringArrange.js @@ -39,4 +39,4 @@ return out_str; } -module.exports = AlternativeStringArrange; \ No newline at end of file +module.exports = AlternativeStringArrange; From fd71eff8bfa23d044ec5948969410dc0167508bd Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Wed, 18 Aug 2021 17:51:12 +0530 Subject: [PATCH 3/5] re-formate the code --- String/AlternativeStringArrange.js | 54 +++++++++++++++--------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/String/AlternativeStringArrange.js b/String/AlternativeStringArrange.js index a484c6cf3..2118bc613 100644 --- a/String/AlternativeStringArrange.js +++ b/String/AlternativeStringArrange.js @@ -5,38 +5,38 @@ * @param {String} str2 second input string * @returns `String` return one alternative arrange string. */ - const AlternativeStringArrange = (str1, str2) => { +const AlternativeStringArrange = (str1, str2) => { - // firstly, check that both inputs are strings. - if (typeof str1 !== 'string' || typeof str2 !== 'string') { - return 'Not string(s)' + // firstly, check that both inputs are strings. + if (typeof str1 !== 'string' || typeof str2 !== 'string') { + return 'Not string(s)' + } + + // output string vlaue. + let out_str = ""; + + // get first string length. + const firstStringLength = str1.length; + // get second string length. + const secondStringLength = str2.length; + // absolute length for oparetion. + let absLenght = firstStringLength > secondStringLength ? firstStringLength : secondStringLength; + + // Iterate the character count until the absolute count is reached. + for (let charCount = 0; charCount < absLenght; charCount++) { + // If firstStringLength is lesser than the charCount it means they are able to re-arange. + if (charCount < firstStringLength) { + out_str += str1[charCount]; } - // output string vlaue. - let out_str = ""; - - // get first string length. - const firstStringLength = str1.length; - // get second string length. - const secondStringLength = str2.length; - // absolute length for oparetion. - let absLenght = firstStringLength > secondStringLength ? firstStringLength : secondStringLength; - - // Iterate the character count until the absolute count is reached. - for(let charCount = 0; charCount < absLenght; charCount++){ - // If firstStringLength is lesser than the charCount it means they are able to re-arange. - if(charCount < firstStringLength){ - out_str += str1[charCount]; - } - - // If secondStringLength is lesser than the charCount it means they are able to re-arange. - if(charCount < secondStringLength){ - out_str += str2[charCount]; - } + // If secondStringLength is lesser than the charCount it means they are able to re-arange. + if (charCount < secondStringLength) { + out_str += str2[charCount]; } + } - // return the output string. - return out_str; + // return the output string. + return out_str; } module.exports = AlternativeStringArrange; From 713ab619855a36d51e71261eacb094526fceac55 Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Wed, 18 Aug 2021 20:25:27 +0530 Subject: [PATCH 4/5] add problem source and explanation reference --- String/AlternativeStringArrange.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/String/AlternativeStringArrange.js b/String/AlternativeStringArrange.js index 2118bc613..5090e6acc 100644 --- a/String/AlternativeStringArrange.js +++ b/String/AlternativeStringArrange.js @@ -1,4 +1,7 @@ // Alternative arrange the two given strings in one string in O(n) time complexity. + +// Problem Source & Explanation: https://www.geeksforgeeks.org/alternatively-merge-two-strings-in-java/ + /** * Alternative arrange the two given strings in one string in O(n) time complexity. * @param {String} str1 first input string From ae74e6b20b4884a2218a8c9f57cb6921bf891196 Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Thu, 19 Aug 2021 00:04:32 +0530 Subject: [PATCH 5/5] reformatted the code with the standard.js code style --- String/AlternativeStringArrange.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/String/AlternativeStringArrange.js b/String/AlternativeStringArrange.js index 5090e6acc..6ff151f7d 100644 --- a/String/AlternativeStringArrange.js +++ b/String/AlternativeStringArrange.js @@ -1,6 +1,6 @@ // Alternative arrange the two given strings in one string in O(n) time complexity. -// Problem Source & Explanation: https://www.geeksforgeeks.org/alternatively-merge-two-strings-in-java/ +// Problem Source & Explanation: https://www.geeksforgeeks.org/alternatively-merge-two-strings-in-java/ /** * Alternative arrange the two given strings in one string in O(n) time complexity. @@ -9,37 +9,36 @@ * @returns `String` return one alternative arrange string. */ const AlternativeStringArrange = (str1, str2) => { - // firstly, check that both inputs are strings. if (typeof str1 !== 'string' || typeof str2 !== 'string') { return 'Not string(s)' } // output string vlaue. - let out_str = ""; + let outStr = '' // get first string length. - const firstStringLength = str1.length; + const firstStringLength = str1.length // get second string length. - const secondStringLength = str2.length; + const secondStringLength = str2.length // absolute length for oparetion. - let absLenght = firstStringLength > secondStringLength ? firstStringLength : secondStringLength; + const absLenght = firstStringLength > secondStringLength ? firstStringLength : secondStringLength // Iterate the character count until the absolute count is reached. for (let charCount = 0; charCount < absLenght; charCount++) { // If firstStringLength is lesser than the charCount it means they are able to re-arange. if (charCount < firstStringLength) { - out_str += str1[charCount]; + outStr += str1[charCount] } // If secondStringLength is lesser than the charCount it means they are able to re-arange. if (charCount < secondStringLength) { - out_str += str2[charCount]; + outStr += str2[charCount] } } // return the output string. - return out_str; + return outStr } -module.exports = AlternativeStringArrange; +module.exports = AlternativeStringArrange