Count how often a character appears in substring ranges

Quick Overview

Answer many range queries that ask how often one lowercase letter appears in an inclusive substring of a string. It tests precomputing per-letter counts so each query runs in constant time, along with careful index handling at the two ends of each range.

Count how often a character appears in substring ranges

Company: Capital One

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given a string `s` of lowercase English letters and a 2D integer array `queries`, where each `queries[i] = [l, r, c]`. For each query, find the number of times the character with ASCII code `c` appears in the substring `s[l..r]`, inclusive on both ends. Return an integer array `answer` where `answer[i]` is the result of the i-th query. ### Function Signature ```python def count_char_in_ranges(s: str, queries: list[list[int]]) -> list[int]: ``` ### Rules - In each query `[l, r, c]`, `l` and `r` are 0-based indices into `s`, and the range includes both `s[l]` and `s[r]`. - `c` is the ASCII code of the letter to count (97 is `a`, 122 is `z`). - Return the answers in the same order as the queries. ### Constraints - `1 <= s.length <= 10^5` - `s` consists of lowercase English letters. - `1 <= queries.length <= 10^5` - `queries[i].length == 3` - `0 <= queries[i][0] <= queries[i][1] < s.length` - `queries[i][2]` is the ASCII code of a lowercase English letter (97 to 122). ### Examples ```text Input: s = "abacaba", queries = [[0, 6, 97], [1, 3, 98], [2, 5, 99], [0, 0, 97]] Output: [4, 1, 1, 1] ``` Code 97 is `a`, which appears 4 times in the whole string. `s[1..3]` is "bac" with one `b` (98), `s[2..5]` is "acab" with one `c` (99), and `s[0..0]` is "a". ```text Input: s = "leetcode", queries = [[0, 7, 101], [1, 4, 116], [0, 3, 108]] Output: [3, 1, 1] ``` Code 101 is `e`, which appears 3 times in "leetcode". `s[1..4]` is "eetc" with one `t` (116), and `s[0..3]` is "leet" with one `l` (108). ```text Input: s = "abc", queries = [[0, 2, 122], [1, 1, 97]] Output: [0, 0] ``` The string has no `z` (122), and the single-character range `s[1..1]` is "b", which holds no `a`.

Overview: Answer many range queries that ask how often one lowercase letter appears in an inclusive substring of a string. It tests precomputing per-letter counts so each query runs in constant time, along with careful index handling at the two ends of each range.

Capital One logo
Capital One
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given a string s of lowercase English letters and a 2D integer array queries, where each queries[i] = [l, r, c].

For each query, find the number of times the character with ASCII code c appears in the substring s[l..r], inclusive on both ends.

Return an integer array answer where answer[i] is the result of the i-th query.

Function Signature

def count_char_in_ranges(s: str, queries: list[list[int]]) -> list[int]:

Rules

  • In each query [l, r, c] , l and r are 0-based indices into s , and the range includes both s[l] and s[r] .
  • c is the ASCII code of the letter to count (97 is a , 122 is z ).
  • Return the answers in the same order as the queries.

Constraints

  • 1 <= s.length <= 10^5
  • s consists of lowercase English letters.
  • 1 <= queries.length <= 10^5
  • queries[i].length == 3
  • 0 <= queries[i][0] <= queries[i][1] < s.length
  • queries[i][2] is the ASCII code of a lowercase English letter (97 to 122).

Examples

Input: s = "abacaba", queries = [[0, 6, 97], [1, 3, 98], [2, 5, 99], [0, 0, 97]]
Output: [4, 1, 1, 1]

Code 97 is a, which appears 4 times in the whole string. s[1..3] is "bac" with one b (98), s[2..5] is "acab" with one c (99), and s[0..0] is "a".

Input: s = "leetcode", queries = [[0, 7, 101], [1, 4, 116], [0, 3, 108]]
Output: [3, 1, 1]

Code 101 is e, which appears 3 times in "leetcode". s[1..4] is "eetc" with one t (116), and s[0..3] is "leet" with one l (108).

Input: s = "abc", queries = [[0, 2, 122], [1, 1, 97]]
Output: [0, 0]

The string has no z (122), and the single-character range s[1..1] is "b", which holds no a.