Minimum Flips for Even Binary Runs
Updated
Problem statement
You are given an even-length binary string password. In one flip, change a single 0 to 1 or a single 1 to 0.
After the flips, the whole string must be split into non-overlapping substrings such that:
- Every substring has even length.
- Every substring contains only one kind of character: all
0s or all1s.
Return the minimum number of flips needed. You may choose any number of substrings, including one.
Function
getMinFlips(password: String) → intExamples
Example 1
password = "1110011000"return = 3Using zero-based positions, flip the zeros at positions 3, 4, and 7. The string becomes 1111111100. Split it into eight 1s followed by two 0s; both parts have even length and one character type.
The original consecutive pairs are 11, 10, 01, 10, and 00. Three pairs differ, and each requires at least one flip, so three flips are optimal.
Example 2
password = "001111"return = 0The string already splits into 00 and 1111, of lengths two and four. Both parts are uniform and have even length, so no flips are needed.
Constraints
2 <= password.length <= 10^5- The length of
passwordis even. passwordcontains only0and1.
More JP Morgan Chase Online Assessment questions
- Find Total CostOA · Seen Oct 2026
- Minimum Meeting RoomsOA · Seen Oct 2026
- Count Dropped RequestsOA · Seen Oct 2026
- Maximize Minimum Deployment DifficultyOA · Seen Oct 2026
- Task SchedulingOA · Seen Oct 2026
- Substring RemovalOA · Seen Oct 2026
- Maximum Game Board ScoreOA · Seen Sep 2026
- Minimum Digit Changes Between String HalvesOA · Seen Sep 2026
- Same Substring Within BudgetOA · Seen Sep 2026
- Invert Ad Visibility BitsOA · Seen Aug 2026
Similar questions at other companies
- Maximum Ones After K OperationsGoldman Sachs · OAGoldman Sachs interview questions
- Minimum Operations to Reduce a Transaction LedgerVisa · OAVisa interview questions
- Prepare NotificationMicrosoft · OAMicrosoft interview questions
- Minimum Changes for Distinct Adjacent CharactersHeadlands Technologies · OAHeadlands Technologies interview questions
- Largest Number With Even Digit FrequenciesDRW · OADRW interview questions
- Minimum Replacements for Unequal Adjacent CharactersIBM · OAIBM interview questions
- Crop Text at a Whole-Word BoundaryAmerican Express · OAAmerican Express interview questions
- Missing WordsVanguard · OAVanguard interview questions
- Digit Changes for a Multiple of 9Nutanix · OANutanix interview questions
- Smallest String After at Most One DeletionLSEG · OALSEG interview questions
Interview reports that mention this question
JP Morgan Chase interview experiences
- JPMorgan Quant Stats Superdays: 3 Experiences (Part 3 of 3)
- JPMorgan Quant Modeling Superdays: 7 Experiences (Part 2 of 3)
- JPMorgan Quant Intern Superday: 19 Experiences (Part 1 of 3)
- JP Morgan Chase Data and AI Intern Superday: 11 Experiences
- JPMorgan Code for Good India 2024: 6 Experiences (Part 5 of 5)