- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have two strings s and a set of query Q. Where Q[i] contains pair (l, r), for each substring of s from l to r, we have to find number of substrings s from x to y where they are similar. Two strings s and t are similar if they follow these rules −

They are of same length

For each pair of indices (i, j), if s[i] is same as s[j], then it must satisfy t[i] = t[j], and similarly if s[i] is not same as s[j], then t[i] and t[j] must be different.

So, if the input is like s = "hjhhbcbk" Q = [(1,2), (2,4)], then the output will be [6, 1] because

- For first query the similar substrings are "hj", "jh", "hb", "bc", "cb" and "bk".
- For first query the similar substring is "jhh"

To solve this, we will follow these steps −

- fp := a new list
- Define a function calc_fingerprint() . This will take s
- dict := a new dictionary, and initially insert key-value pair (s[0], 0)
- fp := "0"
- j := 1
- for i in range 1 to size of s - 1, do
- if s[i] is not present in dict, then
- dict[s[i]] := j
- j = j+1

- fp := fp + string representation of dict[s[i]]

- if s[i] is not present in dict, then
- return integer form of fp
- From the main method, do the following −
- if size of s > 10, then
- for i in range 0 to size of s - 10, do
- x := calc_fingerprint(s[from index i to i+9])
- insert x at the end of fp

- for i in range 0 to size of s - 10, do
- ret := a new list
- for i in range 0 to size of Q - 1, do
- (a, b) := Q[i]
- s1 := substring of s from index a-1 to b-1
- k := 0
- for i in range 0 to size of s - (b-a), do
- if b-a > 9 and fp[a-1] is not same as fp[i], then
- go for next iteration

- dict := a new empty map
- s2 := substring of s from index i to i+(b-a)
- for i in range 0 to b-a, do
- if s2[i] is not in dict, then
- if s1[i] is in values of dict, then
- come out from loop

- dict[s2[i]] := s1[i]

- if s1[i] is in values of dict, then
- if dict[s2[i]] is not same as s1[i], then
- come out from loop

- if s2[i] is not in dict, then
- otherwise,
- k := k + 1

- if b-a > 9 and fp[a-1] is not same as fp[i], then
- insert k at the end of ret

- return ret

Let us see the following implementation to get better understanding −

fp = [] def calc_fingerprint(s): dict = {s[0]: 0} fp = "0" j = 1 for i in range(1, len(s)): if s[i] not in dict: dict[s[i]], j = j, j+1 fp += str(dict[s[i]]) return int(fp) def solve(s, Q): if len(s) > 10: for i in range(0, len(s)-10): fp.append(calc_fingerprint(s[i: i+10])) ret = [] for i in range(len(Q)): a, b = Q[i] s1 = s[a-1:b] k = 0 for i in range(len(s)-(b-a)): if b-a > 9 and fp[a-1] != fp[i]: continue dict = {} s2 = s[i:i+(b-a)+1] for i in range(b-a+1): if s2[i] not in dict: if s1[i] in dict.values(): break dict[s2[i]] = s1[i] if dict[s2[i]] != s1[i]: break else: k += 1 ret.append(k) return ret s = "hjhhbcbk" Q = [(1,2), (2,4)] print(solve(s, Q))

"hjhhbcbk", [(1,2), (2,4)]

[6, 1]

- Related Questions & Answers
- Program to count number of palindromic substrings in Python
- Program to count number of homogenous substrings in Python
- Program to count number of distinct substrings in s in Python
- Python Grouping similar substrings in list
- Program to count number of characters in each bracket depth in Python
- Program to find maximum XOR for each query in Python
- Program to find number of different substrings of a string for different queries in Python
- Program to count maximum score from removing substrings in Python
- Program to find maximum number of non-overlapping substrings in Python
- Python Pandas - Count the number of rows in each group
- Program to find number of substrings with only 1s using Python
- Program to count substrings that differ by one character in Python
- Program to find out the number of pairs of equal substrings in Python
- Count number of occurrences for each char in a string with JavaScript?
- Program to count substrings with all 1s in binary string in Python

Advertisements