Count Substrings That Start With a Letter and End With a Digit

Quick Overview

Count the substrings of a string of lowercase letters and digits that start with a letter and end with a digit, counting equal substrings at different positions separately. It tests reducing a substring count to ordered index pairs and computing it in a single pass.

Count Substrings That Start With a Letter and End With a Digit

Company: Slack

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

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 ```python 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 ```text 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"`. ```text 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. ```text 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.

Overview: Count the substrings of a string of lowercase letters and digits that start with a letter and end with a digit, counting equal substrings at different positions separately. It tests reducing a substring count to ordered index pairs and computing it in a single pass.

Slack logo
Slack
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

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.