All problems
0771MediumHash TableString

Sections Drafted Before Being Approved

Tracked in this browser only
Write code

Trains the technique from

LeetCode 3121Count the Number of Special Characters II

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A document has up to 26 sections, each tagged by one letter of the alphabet. An audit trail arrives as the string log, one character per touch, read left to right in the order the touches happened:

  • a lowercase letter is a draft touch on the section with that tag;
  • the same letter uppercase is an approval touch on that same section.

A section is settled when the trail shows at least one draft touch on it, at least one approval touch on it, and no draft touch on it after its earliest approval touch.

Return how many sections are settled.

Examples

Example 1

Input
log = "ppPqrQR"
Output
3

Section p is drafted at positions 0 and 1 and approved at position 2. Section q is drafted at position 3 and approved at position 5, and section r is drafted at position 4 and approved at position 6. All three have every draft touch ahead of their first approval.

Example 2

Input
log = "mMm"
Output
0

Section m is drafted at position 0, approved at position 1, then drafted again at position 2. That final draft touch comes after the approval, so the section is not settled.

Example 3

Input
log = "ffFgGf"
Output
1

Section g is drafted at position 3 and approved at position 4, so it is settled. Section f is drafted at positions 0, 1 and 5 and approved at position 2, and the touch at position 5 falls after that approval.

Constraints

  • 1 <= log.length <= 2 * 10^5
  • log consists only of lowercase and uppercase English letters.

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def count_promoted_letters(log: str) -> int:
Java
public int countPromotedLetters(String log)
September 7
Apply