Split the String
You are given a string ‘str’ of ‘N’ lowercase alphabets. Your task is to check whether it is possible to split the given string into three non-empty substrings such that one of them is a substring of another two.
For example:
‘str’ = 'abcabab', we can split this string into 3 string a, b, c as a = 'abc', b = 'ab', c = 'abc', we can clearly see that b is a substring of both a and c.
Note:
A substring is a contiguous sequence of characters within a string. For example 'ab', 'b' and 'abc' are the substring of string 'abc', but 'ac' is not a substring of 'abc'.
A non-empty substring means a substring with a size greater than 0.
Input Format:
The first line of input contains an integer 'T' representing the number of test cases.
The first line of each test case contains a single integer ‘N’ denoting the size of the given string.
The second line of each test case contains a string of size ‘N’.
Output Format:
For each test case, print “true” if the given string can be split, or else print “false” without quotes.
The output of each test case will be printed in a separate line.
Constraints:
1 <= T <= 5
1 <= N <= 10 ^ 5
Where ‘T’ is the number of test cases, ‘N’ is the length of the given string, and the given string contains only lowercase English letters.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
CodingNinjas
author
2y
Brute-force
The idea here is to check all possible ways to divide a string into 3 substrings and check if there is a string that is a substring of the other two parts. We can divide a string into 3 non...read more
CodingNinjas
author
2y
Greedy
The idea here is to work with a little bit of intuition. We discussed partitioning of string into 3 parts such as prefix+middle part+suffix. Can we fix the length of the middle string to 1 means...read more
Help your peers!
Add answer anonymously...
Top Nutanix Mts1 interview questions & answers
Popular interview questions of Mts1
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app