FastPrepReverse Binary String with Minimum Shifts

Reverse Binary String with Minimum Shifts

Amazon logoAmazon● MediumNEW GRADOA
Learn

Problem statement

You are given a binary string. Find the minimum number of operations required to reverse it. An operation is defined as: Remove a character from any index and append it to the end of the string.

Function

reverseBinaryString(s: String) → int

Complete the function reverseBinaryString in the editor.

reverseBinaryString has the following parameter:

  1. String s: a binary string

Returns int: the minimum number of operations required to reverse the binary string

Examples

Example 1

s = "00110101"return = 3

Here is one way to reverse the string in 3 operations:

  • 00110101 - 00101011 (index 3 was appended at the end)
  • 00101011 - 01010110 (index 0 was appended at the end)
  • 01010110 - 10101100 (index 0 was appended at the end)

So the answer here is 3 operations.

Constraints

  • 1 ≤ S.length ≤ 1e5

More Amazon problems

See Amazon hiring insights
public int reverseBinaryString(String s) {
  // write your code here
}
s"00110101"
expected3
Checking account…