Thursday Code Puzzler: Longest Common Subsequence
Thursday is code puzzler day here at DZone. The idea is simple: solve the coding problem as efficiently as you can, in any language or framework that you find suitable.
Note: Even though there really is nothing stopping you from finding a solution to this on the internet, try to keep honest, and come up with your own answer. It's all about the participation!
Do you have code puzzlers that you'd like to share with the DZone community? If so, please submit here.
Longest Common Subsequence
The LCS of two groups is the longest group that are common between both and in the same order in each group. Let's look at an example:
Group A: dzone puzzles
Group B: dropzone
The LCS here is dzone
Another example:
Group A: 1234
Group B: 12222354
If you're still confused, there's a good explanation over at Wikipedia.
What you need to do in this challenge is write a method that identifies the LCS between two String parameters.
Catch up on all our previous puzzlers here.






Comments
Vijay Nathani replied on Thu, 2012/08/30 - 4:05am
Groovy solution
def maxSubSequence(a,b) { def convert = { it.toList().subsequences().collect {it.join('')} } def sequencesA = convert(a) sequencesA.retainAll(convert(b)) sequencesA.max {it.size()} } println maxSubSequence('dzone puzzles','dropzone') //prints dopzeprintln maxSubSequence('1234','12222354') //prints 1234
James Nute replied on Thu, 2012/08/30 - 3:27pm
#include <iostream> #include <string> using namespace std; std::string LCS(std::string groupA, std::string groupB){ std::string result; int lengthOfA = groupA.length()-1; int lengthOfB = groupB.length()-1; char endOfA = groupA[lengthOfA]; char endOfB = groupB[lengthOfB]; if(lengthOfA < 0 || lengthOfB < 0){ return result; } if(endOfA==endOfB){ result.insert(0,1,endOfA); result.insert(0,LCS(groupA.substr(0,lengthOfA),groupB.substr(0,lengthOfB))); } else{ std::string lcs1(LCS(groupA.substr(0,lengthOfA+1),groupB.substr(0,lengthOfB))); std::string lcs2(LCS(groupA.substr(0,lengthOfA),groupB.substr(0,lengthOfB+1))); if(lcs1.length() > lcs2.length()){ result.insert(0,lcs1); } else{ result.insert(0,lcs2); } } return result; } int main() { std::string groupA("dzone puzzles"); std::string groupB("dropzone"); cout << "LCS is " << LCS(groupA,groupB) <<endl; return 0; }sun east replied on Thu, 2012/08/30 - 7:29pm
A short but not efficient solution written by Scala:
def lcs(a: String, b: String): String = { if(a.size > 0 && b.size > 0) { val pre = if(a(0) == b(0)) ""+a(0) else "" Seq(lcs(a,b.tail),lcs(a.tail,b), pre + lcs(a.tail,b.tail)).maxBy(_.size) } else "" }Vijay Nathani replied on Thu, 2012/08/30 - 11:22pm
in response to:
James Nute
Awesome. Your recursive algorithm in groovy
def longerString(s1,s2) { s1.size() > s2.size()? s1 : s2 } def lcs(a,b) { if (!a || !b) return "" (a[0] == b[0])? (a[0] + lcs(a.substring(1),b.substring(1))): longerString(lcs(a,b.substring(1)), lcs(a.substring(1),b)) } println lcs('dzone puzzles','dropzone') //prints dopzeprintln lcs('1234','122222354') //prints 1234