Number of Same-End Substrings - Problem
String Pattern Matching Challenge
You're given a string
A same-end substring is any substring where the first and last characters are identical. For example, in the string
Goal: For each query range, efficiently count all possible same-end substrings within that range.
Input: A string
Output: An array where each element is the count of same-end substrings for the corresponding query
You're given a string
s and multiple query ranges. For each query range [li, ri], you need to find how many same-end substrings exist within that substring.A same-end substring is any substring where the first and last characters are identical. For example, in the string
"aba", the same-end substrings are: "a" (position 0), "b" (position 1), "a" (position 2), and "aba" (the entire string).Goal: For each query range, efficiently count all possible same-end substrings within that range.
Input: A string
s and a 2D array queries where each query is [left_index, right_index]Output: An array where each element is the count of same-end substrings for the corresponding query
Input & Output
example_1.py โ Basic Case
$
Input:
s = "abcaa", queries = [[0,2],[1,3],[0,4]]
โบ
Output:
[3, 2, 6]
๐ก Note:
For query [0,2] on substring "abc": same-end substrings are "a", "b", "c" (3 total). For query [1,3] on "bca": same-end substrings are "b", "c", "a" (2 total). For query [0,4] on "abcaa": same-end substrings are "a"(ร5), "b", "c", "aa", "aba", "aca", "abcaa" but only "a"(positions 0,3,4), "b"(position 1), "c"(position 2), "aa"(positions 3-4), "aba"(positions 0-3), "aca"(positions 0-4) giving us 6 total.
example_2.py โ Repeated Characters
$
Input:
s = "aaaa", queries = [[0,3],[1,2]]
โบ
Output:
[10, 3]
๐ก Note:
For "aaaa" with 4 'a's, same-end substrings are: 4 single 'a's + 3 "aa"s + 2 "aaa"s + 1 "aaaa" = 10 total. For substring "aa" (positions 1-2): 2 single 'a's + 1 "aa" = 3 total. This follows the formula n*(n+1)/2 where n is the frequency of each character.
example_3.py โ Single Character
$
Input:
s = "x", queries = [[0,0]]
โบ
Output:
[1]
๐ก Note:
Edge case with single character. The only substring is "x" itself, which is same-end (first and last character are both 'x'). So the answer is 1.
Constraints
- 1 โค s.length โค 105
- 1 โค queries.length โค 105
- queries[i] = [li, ri] where 0 โค li โค ri < s.length
- s consists of only lowercase English letters
Visualization
Tap to expand
Understanding the Visualization
1
Count Character Frequencies
For each query range, count how many times each character appears
2
Apply Combinatorial Formula
For n occurrences of a character, there are n*(n+1)/2 same-end substrings
3
Sum All Contributions
Add up the contributions from all character types in the range
Key Takeaway
๐ฏ Key Insight: Same-end substrings depend only on character frequencies, not positions. Use prefix sums for fast range frequency queries, then apply the triangular number formula f*(f+1)/2 for each character.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code