-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind-all-anagrams-in-a-string.py
More file actions
95 lines (74 loc) · 2.6 KB
/
find-all-anagrams-in-a-string.py
File metadata and controls
95 lines (74 loc) · 2.6 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# /**
# * @param {string} s
# * @param {string} p
# * @return {number[]}
# */
# var findAnagrams = function(s, p) {
# const pLen = p.length;
# const sLen = s.length;
# // Early exit if s is shorter than p
# if (sLen < pLen) {
# return [];
# }
# // Initialize frequency arrays for 'a' to 'z'
# const pCount = new Array(26).fill(0);
# const sCount = new Array(26).fill(0);
# const results = [];
# // Helper function to map a character to its index (0-25)
# const charIndex = (char) => char.charCodeAt(0) - 'a'.charCodeAt(0);
# // Populate frequency count for p
# for (const char of p) {
# pCount[charIndex(char)]++;
# }
# // Populate frequency count for the first window in s
# for (let i = 0; i < pLen; i++) {
# sCount[charIndex(s[i])]++;
# }
# // Check if the initial window matches
# if (sCount.join('') === pCount.join('')) {
# results.push(0);
# }
# // Sliding window: Move the window one character at a time
# for (let i = pLen; i < sLen; i++) {
# // Add the new character to the window
# sCount[charIndex(s[i])]++;
# // Remove the character that is sliding out of the window
# sCount[charIndex(s[i - pLen])]--;
# // Compare frequency arrays
# if (sCount.join('') === pCount.join('')) {
# results.push(i - pLen + 1);
# }
# }
# return results;
# };
from typing import List
class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
pLen = len(p)
sLen = len(s)
# Early exit if s is shorter than p
if sLen < pLen:
return []
# Initialize frequency arrays for 'a' to 'z'
pCount = [0] * 26
sCount = [0] * 26
results = []
# Populate frequency count for p
for char in p:
pCount[ord(char) - ord('a')] += 1
# Populate frequency count for the first window in s
for char in s[:pLen]:
sCount[ord(char) - ord('a')] += 1
# Check if the initial window matches
if sCount == pCount:
results.append(0)
# Sliding window: Move the window one character at a time
for i in range(pLen, sLen):
# Add the new character to the window
sCount[ord(s[i]) - ord('a')] += 1
# Remove the character that is sliding out of the window
sCount[ord(s[i - pLen]) - ord('a')] -= 1
# Compare frequency arrays
if sCount == pCount:
results.append(i - pLen + 1)
return results