Problem · String

Secure Password

Learn this problem
EasyIBM logoIBMFULLTIMEOA
See IBM hiring insights

Problem statement

A password pwd is a binary string of even length. You may flip any character from 0 to 1 or from 1 to 0.

The password is secure when it can be partitioned into non-overlapping contiguous substrings such that:

  • Every substring has positive even length.
  • Every substring contains only 0 characters or only 1 characters.

Return the minimum number of flips needed to make the password secure.

Function

securePassword(pwd: String) → int

Examples

Example 1

pwd = "1110011000"return = 3

Flip three characters so the first eight characters become equal, producing 1111111100. It can be partitioned into 11111111 and 00, so the minimum is 3.

Constraints

  • 2 ≤ pwd.length ≤ 2 × 10^5
  • pwd.length is even.
  • pwd contains only 0 and 1.

More IBM problems

drafts saved locally
public int securePassword(String pwd) {
    // Write your code here
}
pwd"1110011000"
expected3
checking account