forked from saadfareed/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Hactoberfest Contribution Problem#13 * Hactoberfest Contribution Problem#13 * Hacktoberfest Contribution Problem saadfareed#12 * Problem saadfareed#9 Javascript * LeetCode Problem #984
- Loading branch information
1 parent
a165e29
commit c5a984c
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//Name: Raza Mohayyuddin | ||
|
||
//Username: RazaKhan9639 | ||
|
||
//Approach: First create a variable rt to store the required string. After that use if else conditions to to compare the numbers of 'a' and 'b' in a usual order of counting and store the string in rt by appending 2b then a or 2a then b. Time complexity : n, where n is the count of a or b , and space complexity is normal O(1). | ||
|
||
var strWithout3a3b = function (a, b) { | ||
let rt = ""; //Required String | ||
while (a > 0 || b > 0) { | ||
// More 'b', append "bba" | ||
if (a < b) { | ||
if (b-- > 0) { | ||
rt += "b"; | ||
} | ||
if (b-- > 0) { | ||
rt += "b"; | ||
} | ||
if (a-- > 0) { | ||
rt += "a"; | ||
} | ||
} | ||
|
||
// More 'a', append "aab" | ||
else if (b < a) { | ||
if (a-- > 0) { | ||
rt += "a"; | ||
} | ||
if (a-- > 0) { | ||
rt += "a"; | ||
} | ||
if (b-- > 0) { | ||
rt += "b"; | ||
} | ||
} | ||
|
||
// Equal number of 'a' and 'b' | ||
// append "ab" | ||
else { | ||
if (a-- > 0) { | ||
rt += "a"; | ||
} | ||
if (b-- > 0) { | ||
rt += "b"; | ||
} | ||
} | ||
} | ||
return rt; | ||
}; |