question archive Shefin gives you a string S and you have to find a non-empty string P such that: P is a substring of S
Subject:Computer SciencePrice: Bought3
Shefin gives you a string S and you have to find a non-empty string P such that:
For all such possible strings, find the length of the longest string satisfying all the conditions. If no such string is possible, print −1.
A string A is a substring of a string B if A can be obtained from B by deleting several (possibly zero) characters from the beginning and several (possibly zero) characters from the end.
A prefix of a string A, is a substring of A that occurs at the beginning of A. For example, "abig" is a prefix of "abighail", but "big" is not.
A suffix of a string A, is a substring of A that occurs at the end of A. For example, "hail" is a suffix of "abighail", but "hai" is not.
Input Format
Output Format
For each test case, print a single integer. If a string P exists based on the given conditions, print the maximum length of the possible string. Otherwise, print −1.
Constraints
Sample Input 1
2
abcdab
aaa
Sample Output 1
2
-1
Explanation
Test Case 1: The maximum length of the string satisfying all required conditions is 2. The string cd satisfies all the conditions. It can be proven that no string of length greater than 2 exists which can satisfy all the conditions.
Test Case 2: There is no string possible which satisfies all the required conditions. Thus, the answer is −1.