Longest common subsequence: Difference between revisions

Content deleted Content added
Chunes (talk | contribs)
Add Factor example
Line 2,938:
Swift 2.2
===Recursion===
<lang Swift>func rlcs(_ s1: String, _ s2: String) -> String {
let x = s1.characters.count
let y = s2.characters.count
 
if x == 0 || y == 0 {
return ""
} else if s1[s1.startIndex.advancedByindex(xs1.startIndex, -offsetBy: x-1)] == s2[s2.startIndex.advancedByindex(ys2.startIndex, -offsetBy: y-1)] {
return rlcs(String(s1[s1.startIndex..<s1.startIndex.advancedByindex(xs1.startIndex, -offsetBy: x-1)]),
String(s2[s2.startIndex..<s2.startIndex.advancedByindex(ys2.startIndex, -offsetBy: y-1)])) + String(s1[s1.startIndex.advancedByindex(xs1.startIndex, -offsetBy: x-1)])
} else {
let xstr = rlcs(s1, String(s2[s2.startIndex..<s2.startIndex.advancedByindex(ys2.startIndex, -offsetBy: y-1)]))
let ystr = rlcs(String(s1[s1.startIndex..<s1.startIndex.advancedByindex(xs1.startIndex, -offsetBy: x-1)]), s2)
 
return xstr.characters.count > ystr.characters.count ? xstr : ystr
}
}</lang>
 
===Dynamic Programming===