Skip to content

Instantly share code, notes, and snippets.

@whyrusleeping
Created February 25, 2013 03:07
Show Gist options
  • Save whyrusleeping/5027193 to your computer and use it in GitHub Desktop.
Save whyrusleeping/5027193 to your computer and use it in GitHub Desktop.
My solution to the problem i posted earlier
//Check whether the given subsequence occurs in the src
func SubsequenceOccurs(src, seq []byte) bool {
for i := 0; i < len(src) - len(seq); i++ {
if src[i] == seq[0] {
j := 1
for ; j < len(seq); j++ {
if src[i + j] != seq[j] {
break
}
}
if j == len(seq) {
return true
}
}
}
return false
}
func LongestRecurringSubsequence(src []byte) []byte {
l := int(len(src) / 2)
//starting at length l and moving down by one each time
for ; l > 0; l-- {
//For each subsequence S of length l
for i := 0; i < len(src) - l; i++ {
S := src[i:i+l]
//if S occurs in (src - S)
if SubsequenceOccurs(src[:i], S) || SubsequenceOccurs(src[i+l:], S) {
return S
}
}
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment