forked from Adam-Jimenez/binarysearch-editorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagram Partitioning.py
More file actions
24 lines (23 loc) · 818 Bytes
/
Copy pathAnagram Partitioning.py
File metadata and controls
24 lines (23 loc) · 818 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"""
Anagram Partitioning
Recursive solution: each time the frequency of the characters in each character is matched, we do a recursive call and check if the rest of the string is solvable.
"""
from collections import Counter
class Solution:
def solve(self, a, b):
def bt(i=0):
if i == len(a): return True, []
cnta = Counter()
cntb = Counter()
for j in range(i, len(a)):
cnta[a[j]]+=1
cntb[b[j]]+=1
if cnta == cntb:
solved, indexes = bt(j+1)
if solved:
indexes.append(i)
return solved, indexes
return False, []
solved, rev_idx = bt()
if not solved: return []
else: return rev_idx[::-1]