Reverse Words in a String III
Question
Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
Example 1:
Input: "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc"
Note:
In the string, each word is separated by single space and there will not be any extra space in the string.
Approach 1: Canonical Form
Intuition and Algorithm
For each email address, convert it to the canonical address that actually receives the mail. This involves a few steps:
- Separate the email address into a
local
part and therest
of the address. - If the
local
part has a'+'
character, remove it and everything beyond it from thelocal
part. - Remove all the zeros from the
local
part. - The canonical address is
local + rest
.
After, we can count the number of unique canonical addresses with a
Set
structure.
1 | class Solution(object): |
Complexity Analysis
- Time Complexity: O(n), where n is the
total content of
emails
. - Space Complexity: O(n).
- Runtime: 28 ms, faster than 99.72% of Python online submissions for Unique Email Addresses.
- Memory Usage: 11.7 MB, less than 92.68% of Python online submissions for Unique Email Addresses.