Number of Same-End Substrings - Problem
String Pattern Matching Challenge

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
Same-End Substring Counting StrategyMathematical approach using character frequency analysisStep 1: Frequency CountString: "abcaa"Range [0,4]:โ€ข 'a' appears 3 timesโ€ข 'b' appears 1 timeโ€ข 'c' appears 1 timeStep 2: Apply FormulaFor frequency f:Same-end count = f*(f+1)/2โ€ข 'a': 3*(3+1)/2 = 6โ€ข 'b': 1*(1+1)/2 = 1โ€ข 'c': 1*(1+1)/2 = 1Step 3: Sum ResultsTotal same-end substrings:6 + 1 + 1 = 8These include:"a", "a", "a", "b", "c","aa", "aa", "aba"๐Ÿ’ก Key Mathematical InsightThe formula f*(f+1)/2 counts both individual characters and all possible pairsThis is the triangular number sequence: for 3 'a's, we get 3 singles + 2 "aa"s + 1 "aaa" = 6 total
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.
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
43.3K Views
Medium-High Frequency
~25 min Avg. Time
1.3K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen