Given a string $S$, can you remove at most one character so that the remaining string is a double string? A double string is a string of the form $W + W$ for some non-empty string $W$.
The answer is
Let's first try using the simplest solution possible: Try all possible characters to remove (possibly none), and see whether any resulting string is a double string. There are only $|S|+1$ cases to try, because there are only $|S|$ locations to remove a character from, plus $1$ for the "do nothing" scenario. For this to work, we need code that checks whether a string is a double string. Here's a pseudocode that does that:
(Note: We also return
With this function, we can now answer a single test case by trying all $|S|+1$ cases. The following is a pseudocode for this:
Unfortunately, checking whether a string is a double string takes $O(|S|)$ time, so this runs in $(|S|+1)O(|S|) = O(|S|^2)$ time, too slow for the second subtask.
Here are a few observations that will help us find a faster solution:
This gives us the following clues on what we must do:
In the first case, since we can't remove any character, we simply need to check whether $S$ is already a double string to begin with. This takes $O(|S|)$ time so we're good.
In the second case, we still have a problem because we don't know which letter to remove. However, we can use the fact that in a double string, the first half is the same as the second half (by definition). Since we can only remove one character to turn $S$ into a double string, the first "half" of $S$ must already be nearly the same as the second "half" to begin with. (The word "half" here is a bit ambiguous because $|S|$ is odd, but you sorta get the idea.) Specifically, the first and second "halves" must differ from each other by exactly one deletion. This means that the shorter "half" must be a subsequence of the longer "half". Thus, we have reduced the problem to simply checking whether some string is a subsequence of another string!
Now, we still have a problem because we don't know which "half" must be the shorter one. But there are only two possibilities to check (either the first or second half is the shorter one), so it's no problem.
Finally, how do we quickly check whether some string, say $A$, is a subsequence of another string, say $B$? A simple greedy algorithm such as the following will do:
This code runs in $O(|A| + |B|)$ time, and since in our case $|A| + |B| = |S|$, and there are only two cases to check, our algorithm runs in $2\cdot O(|S|) = O(|S|)$.
To summarize, here is our algorithm:
AUTHOR'S AND TESTER'S SOLUTIONS:
This question is marked "community wiki".
asked 13 Mar '16, 14:17
The test were still weak even after changes. Here's my solution https://www.codechef.com/viewsolution/9671989 . I have done hashing and used the concept that the count of only one element %2 should be 1 and then i applied the condition that if has[element]>=100 printf("YES\n"), otherwise copied the string into other string without copying one character of this has[element] one by one and applied the formula for even number of characters. Still my code got accepted!!...
answered 15 Mar '16, 18:54
It was so easy to miss the obvious with this problem. It is unsurprising that the test cases were initially inadequate. The note from the editorial made my error apparent to me:
"Note: We also return false when n == 0 because W must be nonempty from the definition!"
answered 15 Mar '16, 17:00
Simple string processing....MySolution
answered 15 Mar '16, 15:45
thought the same way nice question https://www.codechef.com/viewsolution/9578821
answered 15 Mar '16, 15:48
Can someone please help me and tell where is the problem in my code?
I am getting WA for last test cases of both sub tasks.
answered 15 Mar '16, 18:29
same logic but got wrong answer!!! https://www.codechef.com/viewsolution/9674515
answered 15 Mar '16, 18:35
can any one please tell me where My Solution is failed
answered 15 Mar '16, 23:28
Very easy problem. Consider all the possible cases. Check out my sol https://www.codechef.com/viewsolution/9655225
answered 16 Mar '16, 11:01