Given a string s of lowercase letters and digits, count the substrings that start with a letter and end with a digit.
A substring is contiguous and must have length at least 2. Return the total count of all such substrings. Substrings at different positions are counted separately even when their content is the same.
Function Signature
def count_patterns(s: str) -> int:
Rules
-
A substring is identified by its start index and end index, so two equal strings taken from different positions count twice.
-
A letter is a lowercase English letter
a-z
; a digit is a character
0-9
.
Constraints
-
1 <= len(s) <= 1000
-
s
consists of lowercase English letters and digits (
0-9
).
Examples
Input: s = "a1b2"
Output: 3
The letter at index 0 pairs with the digits at indices 1 and 3, giving "a1" and "a1b2". The letter at index 2 pairs with the digit at index 3, giving "b2".
Input: s = "abc123"
Output: 9
Each of the three letters comes before all three digits, so every letter can pair with every digit: 3 times 3 is 9.
Input: s = "1a1a1"
Output: 3
The valid substrings are "a1" at indices 1 to 2, "a1a1" at indices 1 to 4, and "a1" at indices 3 to 4. The two copies of "a1" sit at different positions, so both count. The leading 1 has no letter before it and adds nothing.