Leetcode # 1657. Determine if Two Strings Are Close
- 2022.12.02
- LeetCode
https://leetcode.com/problems/determine-if-two-strings-are-close/
Solution
Time Complexity: O(len(word1))
Space Complexity: O(1)
※ 英文字母只有26個,所以 sort 在時間上將會花費 O(26 * log(26)) = O(1)
在 Space Complexity 則會花費 O(26) = O(1)
class Solution: def closeStrings(self, word1: str, word2: str) -> bool: char_count = [] for i, word in enumerate([word1, word2]): char_count.append(collections.Counter()) for c in word: char_count[i][c] += 1 return char_count[0].keys() == char_count[1].keys() and\ sorted(char_count[0].values()) == sorted(char_count[1].values())
Last Updated on 2023/08/16 by A1go