## Back To School '18: Letter Frequency

View as PDF

Points: 5 (partial)
Time limit: 1.0s
Python 1.4s
Memory limit: 128M
Java 256M
Python 512M

Author:
Problem types

Yunyi is given a sentence consisting of lowercase Latin letters and spaces, , and he has to determine which language the sentence is written in. One way that he knows of determining the language is by counting letter frequencies in certain sections of the sentence.

Yunyi will give you queries of the form i j c, which asks for the frequency of letter between indices and (inclusive) . Note that spaces ( ) are not considered letters, and therefore will not be queried.

#### Input Specification

The first line will contain the sentence .

The second line will contain the integer .

The next lines will each contain a valid query as defined above.

The sentence will only consist of lowercase latin characters and spaces. There will only be one space between any 2 words, and there will be no leading/trailing spaces.

#### Output Specification

For each query, print the frequency of the letter between indices and (inclusive).

.

No further constraints.

#### Sample Input

this is a very interesting sentence and you will agree
4
1 4 h
6 6 p
15 26 t
1 54 e

#### Sample Output

1
0
2
8

• commented on Sept. 17, 2018, 12:07 a.m.

Any strategy to speed the code up? I can only get 10 out of 100. Is a memory system the way to go?

• commented on Sept. 17, 2018, 8:53 a.m.

Aim for constant time queries, right now you have linear queries in .

• commented on Sept. 17, 2018, 8:12 p.m. edited

You can do constant queries using prefix sum arrays.

• commented on Sept. 17, 2018, 10:55 p.m. edit 6

thanks