FastPrepCount Letters with Ordered Lowercase and Uppercase Occurrences

Count Letters with Ordered Lowercase and Uppercase Occurrences

Marshall Wace logoMarshall Wace● EasyNEW GRADOA

Problem statement

You are given a string letters containing uppercase and lowercase English letters.

Count the different letters that satisfy both conditions:

  • The letter appears at least once in lowercase and at least once in uppercase.
  • Every lowercase occurrence of that letter appears before every uppercase occurrence of the same letter.

Return the number of qualifying letters. Count each letter at most once.

Function

solution(letters: String) → int

Examples

Example 1

letters = "aaAbcCABBc"return = 2

The letters a and b qualify. A lowercase c occurs after an uppercase C, so c does not qualify.

Example 2

letters = "abcABC"return = 3

All three lowercase letters precede their uppercase occurrences.

Example 3

letters = "abABCdef"return = 2

Only a and b appear in both cases. The other letters fail the presence condition.

Example 4

letters = "abcABCabc"return = 0

Each of a, b, and c has a lowercase occurrence after its uppercase occurrence.

Constraints

  • 1 <= letters.length <= 200000.
  • letters contains only English letters a-z and A-Z.

More Marshall Wace problems

See Marshall Wace hiring insights
Saved
public int solution(String letters) {
    // Write your solution here.
}
letters"aaAbcCABBc"
expected2
Checking account…