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