Skip to content

Commit 9f274bc

Browse files
committed
All anagrams
1 parent 162ccee commit 9f274bc

File tree

3 files changed

+162
-1
lines changed

3 files changed

+162
-1
lines changed

codes/all_anagrams.mojo

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
### Find All Anagrams in a String
2+
### Return all start indices of anagrams of string p in string s.
3+
4+
from collections import Dict
5+
6+
7+
fn find_anagrams(s: String, p: String) -> List[Int]:
8+
if len(s) < len(p):
9+
return List[Int]()
10+
pdict = Dict[String, Int]()
11+
sdict = Dict[String, Int]()
12+
for i in range(len(p)):
13+
pdict[p[i]] = 1 + pdict.get(p[i], 0)
14+
sdict[s[i]] = 1 + sdict.get(s[i], 0)
15+
result = List(0) if equals(pdict, sdict) else List[Int]()
16+
left, right = 0, len(p)
17+
while right < len(s):
18+
sdict[s[right]] = 1 + sdict.get(s[right], 0)
19+
sdict[s[left]] = sdict.get(s[left], 1) - 1
20+
if sdict.get(s[left]) and sdict.get(s[left]).value() == 0:
21+
try:
22+
_ = sdict.pop(s[left])
23+
except e:
24+
print(e)
25+
right += 1
26+
left += 1
27+
if equals(pdict, sdict):
28+
result.append(left)
29+
return result
30+
31+
32+
fn equals(read d1: Dict[String, Int], read d2: Dict[String, Int]) -> Bool:
33+
if len(d1) != len(d2):
34+
return False
35+
for each in d1.items():
36+
try:
37+
if each[].value != d2[each[].key]:
38+
return False
39+
except e:
40+
return False
41+
return True
42+
43+
44+
from testing import assert_equal
45+
46+
47+
fn main() raises:
48+
var s: String = "abc"
49+
var p: String = "abc"
50+
result = find_anagrams(s, p)
51+
assert_equal(result, List(0), "Assertion failed")
52+
53+
s = "cba"
54+
p = "abc"
55+
result = find_anagrams(s, p)
56+
assert_equal(result, List(0), "Assertion failed")
57+
58+
s = "cbaa"
59+
p = "abc"
60+
result = find_anagrams(s, p)
61+
assert_equal(result, List(0), "Assertion failed")
62+
63+
s = "cbaacb"
64+
p = "abc"
65+
result = find_anagrams(s, p)
66+
assert_equal(result, List(0, 3), "Assertion failed")
67+
68+
s = "cbaebabacd"
69+
p = "abc"
70+
result = find_anagrams(s, p)
71+
assert_equal(result, List(0, 6), "Assertion failed")
72+
73+
s = "abab"
74+
p = "ab"
75+
result = find_anagrams(s, p)
76+
assert_equal(result, List(0, 1, 2), "Assertion failed")

docs/all_anagrams.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
### Find All Anagrams in a String
2+
### Return all start indices of anagrams of string p in string s.
3+
4+
```python
5+
6+
from collections import Dict
7+
8+
9+
fn find_anagrams(s: String, p: String) -> List[Int]:
10+
if len(s) < len(p):
11+
return List[Int]()
12+
pdict = Dict[String, Int]()
13+
sdict = Dict[String, Int]()
14+
for i in range(len(p)):
15+
pdict[p[i]] = 1 + pdict.get(p[i], 0)
16+
sdict[s[i]] = 1 + sdict.get(s[i], 0)
17+
result = List(0) if equals(pdict, sdict) else List[Int]()
18+
left, right = 0, len(p)
19+
while right < len(s):
20+
sdict[s[right]] = 1 + sdict.get(s[right], 0)
21+
sdict[s[left]] = sdict.get(s[left], 1) - 1
22+
if sdict.get(s[left]) and sdict.get(s[left]).value() == 0:
23+
try:
24+
_ = sdict.pop(s[left])
25+
except e:
26+
print(e)
27+
right += 1
28+
left += 1
29+
if equals(pdict, sdict):
30+
result.append(left)
31+
return result
32+
33+
34+
fn equals(read d1: Dict[String, Int], read d2: Dict[String, Int]) -> Bool:
35+
if len(d1) != len(d2):
36+
return False
37+
for each in d1.items():
38+
try:
39+
if each[].value != d2[each[].key]:
40+
return False
41+
except e:
42+
return False
43+
return True
44+
45+
46+
from testing import assert_equal
47+
48+
49+
fn main() raises:
50+
var s: String = "abc"
51+
var p: String = "abc"
52+
result = find_anagrams(s, p)
53+
assert_equal(result, List(0), "Assertion failed")
54+
55+
s = "cba"
56+
p = "abc"
57+
result = find_anagrams(s, p)
58+
assert_equal(result, List(0), "Assertion failed")
59+
60+
s = "cbaa"
61+
p = "abc"
62+
result = find_anagrams(s, p)
63+
assert_equal(result, List(0), "Assertion failed")
64+
65+
s = "cbaacb"
66+
p = "abc"
67+
result = find_anagrams(s, p)
68+
assert_equal(result, List(0, 3), "Assertion failed")
69+
70+
s = "cbaebabacd"
71+
p = "abc"
72+
result = find_anagrams(s, p)
73+
assert_equal(result, List(0, 6), "Assertion failed")
74+
75+
s = "abab"
76+
p = "ab"
77+
result = find_anagrams(s, p)
78+
assert_equal(result, List(0, 1, 2), "Assertion failed")
79+
80+
```
81+
82+
83+
[Source](https://github.com/ratulb/mojo_programming/blob/main/codes/all_anagrams.mojo)

docs/index.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,6 @@
6363

6464
🟢 [Last word length](last_word_length.md) ➔ Return the length of the last word in a given space-separated string
6565

66-
🟢 [Largest Number](largest_number.md) ➔ Arrange non-negative integers to form the largest possible number and return it as a string.
66+
🟢 [Largest Number](largest_number.md) ➔ Arrange non-negative integers to form the largest possible number and return it as a string.
67+
68+
🟢 [Find All Anagrams in a String](all_anagrams.md) ➔ Return all start indices of anagrams of string p in string s.

0 commit comments

Comments
 (0)