Skip to content

Commit dc2cae9

Browse files
committed
[String] Add a solution to Find All Anagrams in a String
1 parent eff753a commit dc2cae9

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
![Leetcode](./logo.png?style=centerme)
55

66
## Progress
7-
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 321 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
7+
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 322 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
88

99
## Contributors
1010

@@ -93,6 +93,7 @@
9393
[Gas Station](https://leetcode.com/problems/gas-station/)| [Swift](./Array/GasStation.swift)| Medium| O(n)| O(1)|
9494
[Game of Life](https://leetcode.com/problems/game-of-life/)| [Swift](./Array/GameLife.swift)| Medium| O(n)| O(1)|
9595
[Task Scheduler](https://leetcode.com/problems/task-scheduler/)| [Swift](./Array/TaskScheduler.swift)| Medium| O(nlogn)| O(n)|
96+
[]
9697
[Sliding Window Maximum ](https://leetcode.com/problems/sliding-window-maximum/)| [Swift](./Array/SlidingWindowMaximum.swift)| Hard| O(n)| O(n)|
9798
[Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/)| [Swift](./Array/LongestConsecutiveSequence.swift)| Hard| O(n)| O(n)|
9899
[Create Maximum Number](https://leetcode.com/problems/create-maximum-number/)| [Swift](./Array/CreateMaximumNumber.swift)| Hard| O(n^2)| O(n)|
@@ -133,6 +134,7 @@
133134
[One Edit Distance](https://leetcode.com/problems/one-edit-distance/)| [Swift](./String/OneEditDistance.swift)| Medium| O(n)| O(n)|
134135
[Word Pattern](https://leetcode.com/problems/word-pattern/)| [Swift](./String/WordPattern.swift)| Easy| O(n)| O(n)|
135136
[Permutation in String](https://leetcode.com/problems/permutation-in-string/)| [Swift](/.String/PermutationInString.swift)| Medium| O(nm)| O(n)|
137+
[Find All Anagrams in a String](https://leetcode.com/problems/find-all-anagrams-in-a-string/)| [Swift](/.String/FindAllAnagramsInAString.swift)| Medium| O(n)| O(n)|
136138
[Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/)| [Swift](./String/MinimumWindowSubstring.swift)| Hard| O(n^2)| O(n)|
137139
[Longest Substring with At Most Two Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/)| [Swift](./String/LongestSubstringMostTwoDistinctCharacters.swift)| Hard| O(n)| O(n)|
138140
[Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/)| [Swift](./String/LongestSubstringMostKDistinctCharacters.swift)| Hard| O(n)| O(n)|

String/FindAllAnagramsInAString.swift

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/find-all-anagrams-in-a-string/
3+
* Primary idea: Use a dictionary to store characters with frequencies,
4+
* then compare p and them to determine if they are anagrams
5+
*
6+
* Time Complexity: O(n), Space Complexity: O(n)
7+
*
8+
*/
9+
10+
class FindAllAnagramsInAString {
11+
func findAnagrams(_ s: String, _ p: String) -> [Int] {
12+
var res = [Int]()
13+
let s = Array(s)
14+
15+
guard s.count >= p.count else {
16+
return res
17+
}
18+
19+
let pCharsFreq = Dictionary(p.map { ($0, 1) }, uniquingKeysWith: +)
20+
var sCharsFreq = Dictionary(s[0..<p.count].map { ($0, 1)}, uniquingKeysWith: +)
21+
22+
for (i, char) in s.enumerated() {
23+
// check weather they are anagrams
24+
if sCharsFreq == pCharsFreq {
25+
res.append(i)
26+
}
27+
28+
guard i + p.count < s.count else {
29+
break
30+
}
31+
32+
// update the sliding window
33+
if let freq = sCharsFreq[char] {
34+
sCharsFreq[char] = freq - 1
35+
36+
if sCharsFreq[char] == 0 {
37+
sCharsFreq[char] = nil
38+
}
39+
40+
sCharsFreq[s[i + p.count], default: 0] += 1
41+
}
42+
}
43+
44+
return res
45+
}
46+
}

0 commit comments

Comments
 (0)