https://leetcode.com/problems/find-the-original-typed-string-ii/description/?envType=daily-question&envId=2025-07-02 , explain this question..!
视频信息
答案文本
视频字幕
Welcome to LeetCode problem: Find the Original Typed String II. In this problem, we're given a string called 'typed' that represents what someone actually typed on a keyboard. Our goal is to find the original string they intended to type. The key insight is that when typing, people might accidentally press a key multiple times, or they might miss pressing some keys entirely. We need to find the lexicographically smallest possible original string that could have resulted in the given typed string.
Let's understand the two key operations that can happen during typing. First, character duplication: when someone types a character, they might accidentally press the key multiple times, so 'a' becomes 'aa' or 'aaa'. Second, character omission: some characters might be completely missed during typing, so 'abc' could become 'ac' if 'b' is omitted. Our goal is to find the lexicographically smallest original string that could have resulted in the given typed string through these operations.
The solution uses a greedy algorithm approach. We process the typed string from left to right, and for each character, we decide whether to include it in our original string or skip it as a duplicate. The key insight is that we should always take the first occurrence of each character in a group of consecutive identical characters. This greedy choice ensures we get the lexicographically smallest result. Let's trace through the example: for 'abbaca', we include the first 'a', include the first 'b', skip the second 'b' as a duplicate, then include 'a', 'c', and the final 'a', giving us 'abaca'.
Welcome to today's LeetCode problem: Find the Original Typed String II. This is a string processing problem where we need to find the original string from a typed string that may have duplicate characters due to long key presses. The goal is to remove consecutive duplicate characters while preserving the original order. For example, if we have 'aaabbcc', the original string would be 'abc'.
The algorithm is straightforward and efficient. We iterate through the string once, comparing each character with the previous one. If they're different, we add the current character to our result. This way, we automatically skip consecutive duplicates while preserving the first occurrence of each character group. The time complexity is O(n) since we visit each character once, and space complexity is O(1) extra space.
Let's trace through the algorithm step by step with the example 'aaabbcc'. We start with an empty result and add the first character 'a'. Then we compare each subsequent character with the previous one. When we encounter the second 'a', we skip it because it's the same as the previous character. We continue this process, adding 'b' when we first encounter it, skipping the duplicate 'b', adding 'c', and skipping the duplicate 'c'. The final result is 'abc'.
Here's the implementation of our algorithm. The function takes the typed string as input and returns the original string. We start by handling the edge case of an empty string. Then we initialize our result with the first character of the typed string. We iterate through the remaining characters, and for each character, we check if it's different from the previous character. If it is different, we add it to our result. Finally, we join all characters and return the result. This algorithm runs in O(n) time complexity and uses O(1) extra space, making it very efficient.
Let's verify our solution with several test cases. For 'aaabbcc', we get 'abc'. For 'programming', we get 'programing' by removing the duplicate 'm'. We should also consider edge cases like empty strings and single characters. Our algorithm handles all these cases correctly. The time complexity is O(n) because we visit each character exactly once, and the space complexity is O(1) for extra space since we only use a constant amount of additional memory regardless of input size. This makes our solution very efficient for large inputs.
Here's our complete solution for the Find Original Typed String problem. The algorithm is elegantly simple: we iterate through the typed string once, comparing each character with the previous one, and only add characters that are different from their predecessor. This gives us optimal O(n) time complexity with O(1) extra space. Let's verify with our test cases: 'aaabbcc' becomes 'abc', 'programming' becomes 'programing', and so on. The solution handles all edge cases including empty strings and single characters. This approach is both intuitive and efficient, making it an excellent solution for this problem.