Minimum Operations to Reduce a Transaction Ledger
Updated
Problem statement
You are given a lowercase string transactionLedger. In one operation, choose an index whose character is c:
- If another
cexists to its left, remove the nearest such occurrence. - If another
cexists to its right, remove the nearest such occurrence.
The chosen character is not removed. After the removals, the remaining characters close up while preserving their relative order. Continue until the ledger cannot be reduced further.
Return the minimum number of operations needed to reduce the ledger as much as possible. An operation with no removable occurrence does not help and is never required.
Function
minProcessingSteps(transactionLedger: String) → intExamples
Example 1
transactionLedger = "baabacaa"return = 3- Choose the
bat index0. Remove the nextbat index3, obtainingbaaacaa. - Choose the
aat index2. Remove the nearest equal characters at indices1and3, obtainingbacaa. - Choose the
aat index3. Remove the equal characters at indices1and4, obtainingbca.
No character now has another equal occurrence. Three operations are necessary and sufficient.
Example 2
transactionLedger = "aaaa"return = 2First choose an interior a to remove two equal neighbors, leaving aa. A second operation removes one of those two characters. One operation cannot remove all three required occurrences.
Example 3
transactionLedger = "xyz"return = 0The string is already fully reduced because every character is distinct. No operation is needed.
Constraints
1 <= transactionLedger.length() <= 100000transactionLedgercontains only lowercase English letters.
More Visa Online Assessment questions
- Count Prime Values in SubtreesOA · Seen Nov 2026
- Lock CodeOA · Seen Nov 2026
- Minimum-Sum Tree Fill with Unit StepsOA · Seen Nov 2026
- Longest Non-Decreasing Substring After Pair SwapsOA · Seen Oct 2026
- Count Attack Vectors by Divisor RangeOA · Seen Oct 2026
- Count Valid Space-Delimited WordsOA · Seen Oct 2026
- Earliest Common Meeting StartOA · Seen Oct 2026
- Maximum Index-Weighted Sum with Disjoint SwapsOA · Seen Oct 2026
- Maximum Equalized Substrings After One Adjacent SwapOA · Seen Sep 2026
- Minimum Values with Even Powers of TwoOA · Seen Sep 2026
Similar questions at other companies
- Maximum String BeautyBank of America · OABank of America interview questions
- Minimum Substrings Without Repeating CharactersMicrosoft · OAMicrosoft interview questions
- Find Length of Longest Good SubsequenceWalmart · OAWalmart interview questions
- Maximize Palindromic WordsSquarepoint Capital · OASquarepoint Capital interview questions
- Viral Content BalancerTikTok · OATikTok interview questions
- Find Minimum Time RequiredAmazon · OA
- Merging PalindromesOld Mission · OA
- Largest Palindromic Number from Available DigitsMarshall Wace · OAMarshall Wace interview questions
- Worker Task Assignment Part 1 - Balance WorkloadStripe · ONSITE INTERVIEWStripe interview questions
- Reorderable Task Scheduler With CooldownWaymo · PHONE SCREENWaymo interview questions