Skip to content

Commit 24ec82b

Browse files
author
Partho Biswas
committed
438. Find All Anagrams in a String
1 parent 6afd47f commit 24ec82b

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ I have solved quite a number of problems from several topics. See the below tabl
223223
|14| [424. Longest Repeating Character Replacement](https://leetcode.com/problems/longest-repeating-character-replacement/)| [Python](https://tinyurl.com/wu6rdaw/424_Longest_Repeating_Character_Replacement.py)| **[Vid 1](https://tinyurl.com/svyns7x)**, [Vid 2](https://tinyurl.com/rj3yt25), [Art 1](https://tinyurl.com/yx34cd9c), [Art 2](https://tinyurl.com/suxeoj3), [Art 3](https://tinyurl.com/slpeqnd), [Art 4](https://tinyurl.com/sje2no8) | Medium | 📌 Sliding window, Very important |
224224
|15| **[76. Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/)** | [Python](https://tinyurl.com/wu6rdaw/76_Minimum_Window_Substring.py)| **[Vid 1](https://tinyurl.com/tv8lqpa)**, **[Vid 2](https://tinyurl.com/v38h4j4)**, **[Vid 3](https://tinyurl.com/ss5ue49)** | Hard | 📌 Sliding window, Very important |
225225
|16| [727. Minimum Window Subsequence](https://leetcode.com/problems/minimum-window-subsequence/)| [Python](https://tinyurl.com/wu6rdaw/727_Minimum_Window_Subsequence.py)| [Vid 1](https://tinyurl.com/seyskk7), [Art 1](https://tinyurl.com/vs9qedt), [Art 2](https://tinyurl.com/v5tud73) | Hard | 📌 Sliding window and DP, check the DP approach |
226-
|17| [438. Find All Anagrams in a String](https://leetcode.com/problems/find-all-anagrams-in-a-string/)| [Python](https://tinyurl.com/wu6rdaw/438_Find_All_Anagrams_in_a_String.py)| **[Must](https://tinyurl.com/y8jsku3f)**, [Art 1](https://tinyurl.com/wzdp4yp) | Medium | 📌 Sliding window |
226+
|17| [438. Find All Anagrams in a String](https://leetcode.com/problems/find-all-anagrams-in-a-string/)| [Python](https://tinyurl.com/wu6rdaw/438_Find_All_Anagrams_in_a_String.py), [Swift](https://tinyurl.com/wuja3c4/438_Find_All_Anagrams_in_a_String.swift) | **[Must](https://tinyurl.com/y8jsku3f)**, [Art 1](https://tinyurl.com/wzdp4yp) | Medium | 📌 Sliding window |
227227
|18| [567. Permutation in String](https://leetcode.com/problems/permutation-in-string/)| [Python](https://tinyurl.com/wu6rdaw/567_Permutation_in_String.py)| **[Must](https://tinyurl.com/y8jsku3f)** | Medium | 📌 Sliding window |
228228
|19| [844. Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/)| [Python](https://tinyurl.com/wu6rdaw/844_Backspace_String_Compare.py)| --- | Easy | 📌 Two Pointer |
229229
|20| **[809. Expressive Words](https://tinyurl.com/vuv9uud)**| [Python](https://tinyurl.com/wu6rdaw/809_Expressive_Words.py)| [Art 1](https://tinyurl.com/rzlvfn7) | Medium | 📌 Logic ad analytical prolem |
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
// Brute force approach | Time O(n^2) | TLE : 34 / 36 test cases passed.
3+
import Foundation
4+
class Solution {
5+
func findAnagrams(_ s: String, _ p: String) -> [Int] {
6+
var pMap = [Character:Int]()
7+
for char in p {
8+
pMap[char, default: 0] += 1
9+
}
10+
let sArr = Array(s)
11+
var result = [Int]()
12+
for start in 0..<sArr.count {
13+
var need = pMap
14+
for i in start..<sArr.count {
15+
let curChar = sArr[i]
16+
if var needVal = need[curChar], needVal > 0 {
17+
needVal -= 1
18+
if needVal == 0 {
19+
need.removeValue(forKey: curChar)
20+
if need.keys.count <= 0 {
21+
result.append(start)
22+
break
23+
}
24+
} else {
25+
need[curChar] = needVal
26+
}
27+
} else {
28+
break
29+
}
30+
}
31+
}
32+
return result
33+
}
34+
}
35+
36+
// Sliding window | Time O(n)
37+
class Solution {
38+
func findAnagrams(_ s: String, _ p: String) -> [Int] {
39+
if s.count < p.count {
40+
return []
41+
}
42+
let sArr = Array(s)
43+
var result = [Int]()
44+
var (pCounter, sCounter) = ([Character:Int](), [Character:Int]())
45+
for char in p {
46+
pCounter[char, default: 0] += 1
47+
}
48+
for i in 0..<(p.count - 1) {
49+
sCounter[sArr[i], default: 0] += 1
50+
}
51+
52+
for right in (p.count - 1)..<sArr.count {
53+
let rightChar = sArr[right]
54+
sCounter[rightChar, default: 0] += 1
55+
if sCounter == pCounter {
56+
result.append(right - (p.count - 1))
57+
}
58+
let leftChar = sArr[right - (p.count - 1)]
59+
if var prevVal = sCounter[leftChar] {
60+
sCounter[leftChar] = prevVal - 1
61+
if sCounter[leftChar] == 0 {
62+
sCounter.removeValue(forKey:leftChar)
63+
}
64+
}
65+
}
66+
return result
67+
}
68+
}

0 commit comments

Comments
 (0)