Skip to content

Commit ce6eb56

Browse files
author
Chhavi Bansal
committed
[2380] Time Needed to Rearrange a Binary String
1 parent 3334cb2 commit ce6eb56

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
You are given a binary string s. In one second, all occurrences of "01" are simultaneously replaced with "10". This process repeats until no occurrences of "01" exist.
3+
4+
Return the number of seconds needed to complete this process.
5+
6+
7+
8+
Example 1:
9+
10+
Input: s = "0110101"
11+
Output: 4
12+
Explanation:
13+
After one second, s becomes "1011010".
14+
After another second, s becomes "1101100".
15+
After the third second, s becomes "1110100".
16+
After the fourth second, s becomes "1111000".
17+
No occurrence of "01" exists any longer, and the process needed 4 seconds to complete,
18+
so we return 4.
19+
Example 2:
20+
21+
Input: s = "11100"
22+
Output: 0
23+
Explanation:
24+
No occurrence of "01" exists in s, and the processes needed 0 seconds to complete,
25+
so we return 0.
26+
27+
28+
Constraints:
29+
30+
1 <= s.length <= 1000
31+
s[i] is either '0' or '1'.
32+
*/
33+
34+
35+
/**
36+
* @param {string} s
37+
* @return {number}
38+
*/
39+
var secondsToRemoveOccurrences = function(s) {
40+
let result = 0;
41+
while (true) {
42+
const replaced = s.replaceAll('01', '10');
43+
if (s === replaced) return result;
44+
s = replaced;
45+
result += 1;
46+
}
47+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const assert = require('assert');
2+
const secondsToRemoveOccurrences = require('../../LeetcodeProblems/Algorithms/Time_Needed_Rearrange_Binary_String').secondsToRemoveOccurrences;
3+
4+
function test() {
5+
assert.equal(secondsToRemoveOccurrences("0110101"));
6+
assert.equal(secondsToRemoveOccurrences("11100"))
7+
};
8+
9+
module.exports.test = test

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ To run a specific problem in your console run `node <problem_file_path>` (e.g.
6262
| [Construct Binary Tree from Preorder and Inorder Traversal ](/LeetcodeProblems/Algorithms/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal.js) | Medium | https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ |
6363
| [Lowest Common Ancestor of a Binary Tree ](/LeetcodeProblems/Algorithms/Lowest_Common_Ancestor_of_a_Binary_Tree.js) | Medium | https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ |
6464
| [Maximum Sum of an Hourglass](/LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum.js) | Medium | https://leetcode.com/problems/maximum-sum-of-an-hourglass/ |
65+
| [Time Needed to Rearrange a Binary String] (/LeetcodeProblems/Algorithms/Time_Needed_Rearrange_Binary_String.js)| Medium | https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string/ |
6566
| [Flood Fill ](/LeetcodeProblems/Algorithms/Flood_Fill.js) | Easy | https://leetcode.com/problems/flood-fill/ |
6667
| [Implement stack using queues ](/LeetcodeProblems/Algorithms/Implement_stack_using_queues.js) | Easy | https://leetcode.com/problems/implement-stack-using-queues/ |
6768
| [Number of Segments in a String ](/LeetcodeProblems/Algorithms/Number_of_Segments_in_a_String.js) | Easy | https://leetcode.com/problems/number-of-segments-in-a-string/ |

0 commit comments

Comments
 (0)