Skip to content

Instantly share code, notes, and snippets.

@oisdk

oisdk/subStrings.Swift

Last active Aug 29, 2015
Embed
What would you like to do?
extension Sliceable where Generator.Element : Equatable, Index.Distance == Int {
func lComSubStr<
S: Sliceable where
S.Generator.Element == Generator.Element,
S.Index.Distance == Int
>(with: S) -> SubSlice {
var (len, end) = (0, 0)
let empty = Array(Repeat(count: with.count + 1, repeatedValue: 0))
var mat: [[Int]] = Array(Repeat(count: self.count + 1, repeatedValue: empty))
for (i, sLett) in self.enumerate() {
for (j, tLett) in with.enumerate() where tLett == sLett {
let curLen = mat[i][j] + 1
mat[i + 1][j + 1] = curLen
if curLen > len {
len = curLen
end = i
}
}
}
return self[advance(self.startIndex, (end + 1) - len)...advance(self.startIndex, end)]
}
}
extension Sliceable where Generator.Element : Equatable, SubSlice.Generator.Element == Generator.Element {
func contains<
C: CollectionType where
C.Generator.Element == Generator.Element,
Index.Distance == C.Index.Distance
> (col: C) -> Bool {
return lazy(self.startIndex...advance(self.startIndex, self.count - col.count))
.contains { self[$0..<advance($0, col.count)].elementsEqual(col) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.