Skip to content

Commit df014ca

Browse files
committed
Adds brute force string search
1 parent 84e306b commit df014ca

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
extension String {
2+
func indexOf(_ pattern: String) -> String.Index? {
3+
4+
for i in self.characters.indices {
5+
var j = i
6+
var found = true
7+
for p in pattern.characters.indices {
8+
if j == self.characters.endIndex || self[j] != pattern[p] {
9+
found = false
10+
break
11+
} else {
12+
j = self.characters.index(after: j)
13+
}
14+
}
15+
if found {
16+
return i
17+
}
18+
}
19+
return nil
20+
}
21+
}
22+
23+
// A few simple tests
24+
25+
let s = "Hello, World"
26+
s.indexOf("World") // 7
27+
28+
let animals = "🐶🐔🐷🐮🐱"
29+
animals.indexOf("🐮") // 6
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='osx'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

SwiftAlgorithmClub/BruteForceStringSearch.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SwiftAlgorithmClub/SwiftAlgorithmClub.xcworkspace/contents.xcworkspacedata

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)