Problem · String

How Many Flips?

Learn this problem
MediumJPMorgan ChaseFULLTIMEOA

Problem statement

Start with an initial string of zeros. Choose any digit to flip. When a digit is flipped, its value and those to the right switch state between 0 and 1. Given a target string of binary digits, determine the minimum number of flips required to achieve the target.

Function

minimumFlips(target: String) → int

Complete the function minimumFlips in the editor below.

minimumFlips has the following parameter(s):

  1. string target: a string of 0s and 1s to match

Returns

int: the minimum number of flips needed to obtain the target string

Examples

Example 1

target = "01011"return = 3

Start with a string of 5 zeros, the same length as the target.

Initial String -> 00000

Flip the 3rd digit -> 00111

Flip the 2nd digit -> 01000

Flip the 4th digit -> 01011

3 flips are required to reach the target. The return value is 3.

Constraints

Unknown for now

More JPMorgan Chase problems

drafts saved locally
public int minimumFlips(String target) {
    // write your code here
}
target"01011"
expected3
checking account