Skip to content

Commit 96dd78c

Browse files
committed
Runtime: 5068 ms (Top 15.23%) | Memory: 14.3 MB (Top 64.62%)
1 parent a201684 commit 96dd78c

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
1+
# Runtime: 5068 ms (Top 15.23%) | Memory: 14.3 MB (Top 64.62%)
12
class Solution: #839. Similar String Groups
23
def numSimilarGroups(self, strs: List[str]) -> int:
34
#memo
45
visited = set()
56
count = 0
67
for i in range(len(strs)):
78
if i not in visited:
8-
#dfs
9+
#dfs
910
self.dfs(strs, i, visited)
1011
#add a new connected area
1112
count += 1
1213
return count
13-
14+
1415
#dfs to search the similar string from 0 to n-1
1516
def dfs(self, strs, i, visited):
1617
#add current string to memo
1718
visited.add(i)
1819
for j in range(len(strs)):
1920
if self.isSimilar(strs[i], strs[j]) and j not in visited:
2021
self.dfs(strs, j , visited)
21-
22-
# calculate the similarity of two strings
22+
23+
# calculate the similarity of two strings
2324
def isSimilar(self, str1, str2):
2425
diff_count = 0
2526
for i in range(len(str1)):
2627
if str1[i] != str2[i]:
2728
diff_count += 1
28-
return diff_count <= 2
29+
return diff_count <= 2

0 commit comments

Comments
 (0)