Skip to content

Commit 69e2998

Browse files
committed
refactor(strings, similar-string-groups): add length check
1 parent dc02f1e commit 69e2998

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

datastructures/sets/union_find/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ def union(self, i: int, j: int) -> bool:
2222
Merges the sets containing elements 'i' and 'j'.
2323
Returns True if a merge occurred, False if they were already in the same set.
2424
"""
25-
root_i = self.find(i) # 0
26-
root_j = self.find(j) # 1
25+
root_i = self.find(i)
26+
root_j = self.find(j)
2727

2828
if root_i != root_j:
2929
# Union by rank: attach the smaller tree to the larger tree

pystrings/similar_string_groups/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ def are_similar(s1, s2):
6767

6868
def num_similar_groups_2(strs: List[str]) -> int:
6969
n = len(strs)
70-
uf = DisjointSetUnion(n)
70+
if n == 0:
71+
return 0
72+
73+
uf = UnionFind(n)
7174

7275
for i in range(n):
7376
for j in range(i + 1, n):

0 commit comments

Comments
 (0)