Show problems as
Classic problem #3121MediumHash TableString

Count the Number of Special Characters II

Real-world version: Sections Drafted Before Being Approved
Tracked in this browser only
Read Count the Number of Special Characters II on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

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