I've been a zone leader with DZone since 2008, and I'm crazy about community. Every day I get to work with the best that JavaScript, HTML5, Android and iOS has to offer, creating apps that truly make at difference, as principal front-end architect at Avego. James is a DZone Zone Leader and has posted 639 posts at DZone. You can read more from them at their website. View Full User Profile

Thursday Code Puzzler: Longest Common Subsequence

  • submit to reddit

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.


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.max {it.size()}
println maxSubSequence('dzone puzzles','dropzone') //prints dopze

println maxSubSequence('1234','12222354') //prints 1234 

James Nute replied on Thu, 2012/08/30 - 3:27pm

Not convinced it's the most efficient way to go, but its one way to go about it in C++ 
#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;

		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()){
	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 ""
	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 dopze

println lcs('1234','122222354') //prints 1234 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.