-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode 1657: Determine if Two Strings Are Close.py
More file actions
42 lines (32 loc) · 1.31 KB
/
Leetcode 1657: Determine if Two Strings Are Close.py
File metadata and controls
42 lines (32 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Leetcode 1657: Determine if Two Strings Are Close
# https://leetcode.com/problems/determine-if-two-strings-are-close/
#Method: count freq and then make sure the freq matches 2 conditions
class Solution(object):
def closeStrings(self, word1, word2):
"""
:type word1: str
:type word2: str
:rtype: bool
"""
if len(word1) != len(word2):
return False
#counting frequency of each character in the list
freq1 = {}
freq2 = {}
for i in range(len(word1)):
freq1[word1[i]] = freq1.get(word1[i], 0) + 1
freq2[word2[i]] = freq2.get(word2[i], 0) + 1
# Condition 1: Both words must contain the same unique characters
if set(freq1.keys()) != set(freq2.keys()):
return False
freq1_count = {}
freq2_count = {}
# Condition 2: The frequency counts must match
# Ex: how many times the freq of 2 appears in freq1 & freq2
for i in freq1.values():
freq1_count[i] = freq1_count.get(i,0) + 1
for i in freq2.values():
freq2_count[i] = freq2_count.get(i,0) + 1
return freq1_count == freq2_count
#time complexity: O(N)
#space complexity: O(1) cuz the set will only be 26 letters in the alphabet