Ctrl K

Small Letters Come First

Medium
You are given a string text made of English letters in both cases. Call a letter *tidy* if it appears in text both in lowercase and in uppercase, and **every** lowercase occurrence comes before the **first** uppercase one. Return the number of tidy letters.
Example 1
text="xyXzYZ" → 3

For each of x, y and z every lowercase copy comes before the first capital, so the answer is 3.

Example 2
text="mMmN" → 0

For m a lowercase copy appears again after the capital M, and n has no lowercase copy at all. The answer is 0.

Example 3
text="ppqPQq" → 1
Constraints
  • 1 ≤ len(text) ≤ 2 · 10⁵
  • text consists only of lowercase and uppercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLongest Repeat-Free Stretch MediumNumber in Roman Numerals MediumKeypad Words MediumAnagram Buckets MediumSplit Into Dictionary Words Medium
loading Python…
Input
text="xyXzYZ"
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 103 tests on the server, hidden ones included.