You are given a string ‘STR’ consisting of English lowercase letters.
Your task is to find out all the generalised abbreviations of ‘STR’ and print an array/list of these abbreviations sorted in increasing order.
Note:
A string is said to be a generalised abbreviations string of ‘STR’ if we remove a substring of length ‘X’ from ‘STR’ and put the number ‘X’ at the place of the removed substring.
We can not remove two consecutive substrings or we can say generalised abbreviations will never have two consecutive numbers.
For example:
If ‘STR’ = “abc”,
Sorted generalized abbreviations of ‘STR’ are: [“1b1”, “1bc”, “2c”, “3”, “a1c”, “a2”, “ab1”, “abc”].
Input Format:
The first line of input contains a single integer T, representing the number of test cases.
Then the T test cases follow.
The first and the only line of each test case contains a string ‘STR’.
Output format:
For every test case, print all the generalised abbreviations of ‘STR’ separated by a single space.
The output of each test case is printed in a separate line.
Note :
You don’t have to print anything. It has already been taken care of. Just implement the given function.
Constraints:
1<= T <=10
1<= |STR| <= 20
Where |STR| is length of String 'STR'.
Time limit: 1 sec
2
ab
xyz
1b 2 a1 ab
1y1 1yz 2z 3 x1z x2 xy1 xyz
For test case 1:
"ab" can be written as {1b, 2, a1, ab}.
For test case 2:
"xyz" can be written as {1y1, 1yz, 2z, 3, x1z, x2, xy1, xyz}.
2
n
code
1 n
1o1e 1o2 1od1 1ode 2d1 2de 3e 4 c1d1 c1de c2e c3 co1e co2 cod1 code
For test case 1:
"n" can be written as {1, n}.
For test case 2:
"code" can be written as {1o1e, 1o2, 1od1, 1ode, 2d1, 2de, 3e, 4, c1d1, c1de, c2e, c3, co1e, co2, cod1, code}.
Recursively trying to get all the string by noting count of current string length.
Approach: The idea is to use backtracking to generate all the generalised abbreviations of the given string.
Complete Algorithm:
O(2 ^ N), where N is the size of string ‘STR’.
As for each recursive call, we are making further 2 calls and increasing ‘INDEX’ by 1 till it reaches ‘N’ thus raising time complexity to O(2 ^ N).
O(N), where N is the size of string ‘STR’.
As total number of strings will be 2 ^ N and Space used by the recursion stack is also O(N). Thus Space complexity to O(N).