0

I want to find the occurrences of a character in a string for n queries: For example the string is: "i_love_mathematics" and the task is to find the occurrence of:

'i' in range:

          1-4(a substring starting from 1st character and ending at 4th)
          2-5
          3-10

'_' in range:

           1-10
           3-9

The output would be:

           1
           0
           0
           2
           1

The similar question was to find the number of occurrences of a character in a string but the complexity for that was O(N) but in this case, if I do that it would result in very high complexity, is there a data structure that could be used to solve this problem?

aroma
  • 1,370
  • 1
  • 15
  • 30
  • Complexity would be `O(q * n)`, where `q` is the number of queries and `n` is the length of the string if you go for a naive implementation. Alternatively you could use a tree to reduce the complexity to `O(q log n)` with space-complexity of `O(n)`. Or you could use a table that maps each character to a list of indices on which a certain character occurs. Complexity would stay the same. –  Mar 03 '17 at 14:44

2 Answers2

2

We will keep the number of occurrences of each character at each position, for example the string abacaba

    a b a c a b a
|  |1|2|3|4|5|6|7
a | 1 1 2 2 3 3 4
b | 0 1 1 1 1 2 2
c | 0 0 0 1 1 1 1

Now, if we want to answer a query we do the following

Letter 'a' in range 3-5

We do a at position 5 minus number of occurrences before position 3, that is a[5]-a[3-1]=3-1=2 there are 2 occurrences of the letter 'a' in range 3 to 5

EmmanuelAC
  • 141
  • 3
0

Time complexity for searching the range for each and every query would be O(n*q)

n: length of string

q: no.of queries

But there is a better way to do it You can use segment trees.

Time complexity for tree construction is O(N) and for each query is O(log(n))

So, for q queries Time complexity would be O(q*log(n))

Naveen Kakani
  • 121
  • 10