491. Number of Same-End Substrings
Given a 0-indexed string s and a 2D array queries, where each queries[i] = [li, ri] represents a substring s[li..ri], the task is to determine the number of same-end substrings for each query.
A same-end substring is defined as a contiguous non-empty substring whose first and last character are the same.
Return an array ans where ans[i] represents the number of same-end substrings in s[li..ri].
Example 1:
Input: s = "abbaac", queries = [[0,1], [1,4], [2,5], [0,5]]
Output: [2, 6, 5, 10]
Explanation:
For each query, count of same-end substrings:
"ab" → 2 {"a", "b"}
"bbaa" → 6 {"b", "b", "a", "a", "bb", "aa"}
"baac" → 5 {"b", "a", "a", "c", "aa"}
"abbaac" → 10 {"a", "b", "b", "a", "a", "c", "bb", "aa", "abba", "abbaa"}
Example 2:
Input: s = "xyz", queries = [[0,2]]
Output: [3]
Explanation:
"xyz" → 3 {"x", "y", "z"}
Now Your Turn!
Pick the correct output for the given inputInput: s = "aaaa", queries = [[0,3], [1,3], [0,2]]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 2 <= s.length <= 3 × 104
- s consists only of lowercase English letters.
- 1 <= queries.length <= 3 × 104
- 0 <= li <= ri < s.length