Problem Statement
A string S is called a square if there is some string T such that S = T + T.
For example, the strings "", aabaab" and "xxxx" are squares, but "a", "aabb" and "aabbaa" are not.
You are given a String s. Find the longest square string that can be obtained from s by erasing some (possibly none, possibly all) of its characters. In other words, we are looking for the longest square that occurs in s as a subsequence. Return the length of that square.
Note that the answer is well-defined, as the square "" (the empty string) will always occur in s as a subsequence.
You are given a String s. Find the longest square string that can be obtained from s by erasing some (possibly none, possibly all) of its characters. In other words, we are looking for the longest square that occurs in s as a subsequence. Return the length of that square.
Note that the answer is well-defined, as the square "" (the empty string) will always occur in s as a subsequence.
Definition
- Class:
- RepeatStringEasy
- Method:
- maximalLength
- Parameters:
- String
- Returns:
- int
- Method signature:
- int maximalLength(String s)
- (be sure your method is public)
Constraints
- s will contain between 1 and 50 characters, inclusive.
- Each character in s will be a lowercase English letter ('a'-'z').
Examples
"frankfurt"
Returns: 4
The longest square that occurs in "frankfurt" is "frfr". Its length is 4."single"
Returns: 0
The letters in the string "single" are all distinct. Hence, the only square that occurs in this string is "". The length of this square is zero."singing"
Returns: 6
"aababbababbabbbbabbabb"
Returns: 18
"x"
Returns: 0