#K66702. Distinct Characters in Substrings
Distinct Characters in Substrings
Distinct Characters in Substrings
You are given a string s and Q queries. Each query is represented by two integers l and r. For each query, you need to find the number of distinct characters in the substring s[l:r] (inclusive).
Note: If the query is invalid (i.e. if s is empty, or l is negative, or r is greater than or equal to the length of s, or l is greater than r), output \( -1 \).
The formula for the number of distinct characters in a valid substring can be expressed in \( \LaTeX \) as follows:
\[ \text{result} = |\{ c : c \in s[l:r+1] \}| \]Your task is to implement a solution which reads from stdin
and writes the result for each query to stdout
.
inputFormat
The input is read from stdin
. It consists of:
- A line containing the string s.
- A line containing an integer Q, the number of queries.
- Q subsequent lines, each containing two space-separated integers l and r representing a query.
It is guaranteed that s will consist of printable characters.
outputFormat
For each query, output a single line containing one integer: the number of distinct characters in the substring s[l:r] if the query is valid; otherwise, output \( -1 \).
The output is written to stdout
.
abacaba
4
0 3
1 4
2 5
0 6
3
3
3
3
</p>