Last active
August 29, 2015 14:05
-
-
Save thomasjpfan/7e9ca7f773ffaf5106a3 to your computer and use it in GitHub Desktop.
Swift Challenge #3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Takes into account of multiple matching substrings and returns an array of String.Indexes | |
extension String { | |
func findIndexOf(subString: String) -> [String.Index]?{ | |
let selfLength = countElements(self) | |
let subStringLength = countElements(subString) | |
if subStringLength == 0 || selfLength == 0 || selfLength < subStringLength { | |
return nil | |
} | |
let selfArray = Array(self) | |
let subStringArray = Array(subString) | |
var indexes: [Int] = [] | |
for (i, letter) in enumerate(selfArray[0...selfLength-subStringLength]){ | |
if equal(subStringArray, selfArray[i..<subStringLength+i]){ | |
indexes.append(i) | |
} | |
} | |
return indexes.count != 0 ? indexes.map{advance(self.startIndex, $0)} : nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment