Largest Number With Even Digit Frequencies
Updated
Problem statement
You are given a string digits containing only the digits 1 and 2.
Delete zero or more characters without changing the relative order of the characters that remain. In the resulting string, both 1 and 2 must occur an even number of times; zero occurrences of a digit is allowed.
Return the numerically largest valid remaining string.
Function
solution(digits: String) → StringExamples
Example 1
digits = "121212"return = "2121"Each digit occurs three times, so one 1 and one 2 must be removed. Removing the first 1 and the last 2 produces 2121, the largest valid result.
Example 2
digits = "2121122"return = "221122"The string has three 1s and four 2s. Removing the first 1 makes both frequencies even and yields 221122.
Example 3
digits = "1111"return = "1111"The four 1s already have even frequency, so keeping every character gives the largest result.
Constraints
3 <= digits.length <= 200000.digitscontains only1and2.
More DRW Online Assessment questions
- Count Balanced Nodes in a Rooted TreeOA · Seen Sep 2026
- Shortest Path Around Rectangular ObstaclesOA · Seen Sep 2026
- 2020 Account Balance with Monthly Card FeesOA · Seen Aug 2026
- Nested In-Memory Database TransactionsOA · Seen Aug 2026
- Password Requirement CheckOA · Seen Aug 2026
- Remove Excess Consecutive CharactersOA · Seen Aug 2026
- Last Round for Each PlayerOA · Seen Jul 2026
- Shortest Compressed Length After RemovalOA · Seen Jul 2026
- Doctor Appointment Slot AssignmentOA · Seen Jul 2026
- Maximum Even-Sum Neighboring PairsOA · Seen Jul 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
- Minimum Replacements for Unequal Adjacent CharactersIBM · OAIBM interview questions
- Crop Text at a Whole-Word BoundaryAmerican Express · OAAmerican Express interview questions
- Minimum Flips for Even Binary RunsJP Morgan Chase · OAJP Morgan Chase 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