You are given a string of size ‘N’ consisting of only characters ‘a’,’c’,’g’ and ’t’. You have to find all the sub-strings of the given string of length 10 that is occurring more than once in the given string.
The first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of each test case consists of a single integer ‘N’ denoting the size of the string.
The next line consists of ‘N’ length string consisting of only ‘a’,’c’,’g’ and ’t’.
Output Format:
For each test case, return all possible sub-strings of size 10 that occurs more than one time. You may return the substrings in any order.
1 <= ’T’ <= 50
1 <= ’N’ <= 5* 10^3
s[i] = {‘a’,‘c’,’g’ ,’t’ }
Time Limit: 1 sec
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
2
12
aaaaaaaaaaab
20
aaagggccctaaagggccct
aaaaaaaaaa
aaagggccct
For First Test-case total number of substrings of size 10 are:-
[aaaaaaaaaa,aaaaaaaaaa,aaaaaaaaab]
From the above substrings, aaaaaaaaaa occurs more than once.
For SecondTestcase total number of substrings of size 10 are:-
[aaagggccct,aagggcccta,agggccctaa,gggccctaaa,ggccctaaag,gccctaaagg, ccctaaaggg, cctaaagggc ,ctaaagggcc ,taaagggccc ,aaagggccct]
From the above substrings, aaagggccct occurs more than once.
2
11
ggggggggggg
20
aaaaagggggaaaaaggggg
gggggggggg
aaaaaggggg
Store the count of substrings of length 10.
The main idea is to store the count of all substrings.
Algorithm:
O(N) where ‘N’ is the length of the string.
We are running nested loops which will take N * 10 steps where n is the length of the string. Hence time complexity is O(N).
O(N), where ‘N’ is the length of the string.
We are storing all substrings of size 10 and there are total N-9 substrings of size 10 where n is the length of the string. Hence space complexity is O(N).