Skip to content

Commit

Permalink
Raza khan9639 (saadfareed#53)
Browse files Browse the repository at this point in the history
* Hactoberfest Contribution Problem#13

* Hactoberfest Contribution Problem#13

* Hacktoberfest Contribution Problem saadfareed#12

* Problem saadfareed#9 Javascript

* LeetCode Problem #984
  • Loading branch information
RazaKhan9639 authored Oct 12, 2022
1 parent a165e29 commit c5a984c
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions JS/Restrained3a3b.js
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;
};

0 comments on commit c5a984c

Please sign in to comment.