Skip to content

Instantly share code, notes, and snippets.

@oisdk
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oisdk/24a853d92af26049fc27 to your computer and use it in GitHub Desktop.
Save oisdk/24a853d92af26049fc27 to your computer and use it in GitHub Desktop.
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