Editorial
Let be the set of characters that occur at least once in .
Every character of a common subsequence must occur in , so it must belong to . Thus, the answer cannot exceed the number of characters of that belong to .
Conversely, take every character of that belongs to , preserving its order, and call the resulting string . Each character of occurs somewhere in . After any already selected position of , the next copy of contains the desired character, so all characters of can be selected one by one. Therefore, is a subsequence of .
Hence, the answer is exactly the number of characters of that occur at least once in .
The time complexity is and the extra space complexity is .
Solution written by GPT5.6