FastPrepMinimum Operations to Reduce a Transaction Ledger

Minimum Operations to Reduce a Transaction Ledger

Visa logoVisa● EasyFULLTIMEOA
Learn

Problem statement

You are given a lowercase string transactionLedger. In one operation, choose an index whose character is c:

  • If another c exists to its left, remove the nearest such occurrence.
  • If another c exists 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) → int

Examples

Example 1

transactionLedger = "baabacaa"return = 3
  1. Choose the b at index 0. Remove the next b at index 3, obtaining baaacaa.
  2. Choose the a at index 2. Remove the nearest equal characters at indices 1 and 3, obtaining bacaa.
  3. Choose the a at index 3. Remove the equal characters at indices 1 and 4, obtaining bca.

No character now has another equal occurrence. Three operations are necessary and sufficient.

Example 2

transactionLedger = "aaaa"return = 2

First 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 = 0

The string is already fully reduced because every character is distinct. No operation is needed.

Constraints

  • 1 <= transactionLedger.length() <= 100000
  • transactionLedger contains only lowercase English letters.

More Visa problems

See Visa hiring insights
public int minProcessingSteps(String transactionLedger) {
    // Write your code here.
}
transactionLedger"baabacaa"
expected3
Checking account…