Number of Same-End Substrings
For each query range, count the substrings whose first and last letters match. Prefix counts per letter answer each query directly.
For each query range, count the substrings whose first and last letters match. Prefix counts per letter answer each query directly.