Site contents and design © Brian Beckett 2008
Contact: brian {at} worthlesscheese {dot} com

Valid HTML 4.01 Strict

Valid CSS!

Longest Common Substrings - a Java code sample

Description

This is a short program to find the longest common substrings in two strings. There is also an option to print out all of the common substrings.

Raison d'être

My friend Louise has a job at Morgan Stanley. They've given her a bunch of exercises to do before she starts work. This was one of them.

From: Louise
To: Brian

I wrote a program yesterday that finds the longest common substring in 2 strings for MS. It's not as easy as you think!

From: Brian
To: Louise

Sounds like an interesting challenge - I'll have a look at it later, if I remember!

From: Brian
To: Louise

Ok, what's next? :)
Writing the args[] handling code took longer than the actual method!

Development Plans

Development has finished.

Development History

23/07/08 - Louise mentioned the exercise to me
24/07/08 - I did it.

System Requirements

Downloads

CommonSubstrings.java

Instructions

Download the file linked above to and open it in a text editor.

To compile the program, run "javac CommonSubstrings.java" from a command prompt in the same directory as the file. You will need the Java SDK installed for this to work.

To run the program, run the command "java CommonSubstrings $arg1 $arg2 $arg3" from a command prompt in the same directory as the compiled .class file. $arg1, $arg2 and $arg3 are arguments to the program. It needs two Strings, (e.g. java CommonSubstring "test" "teest") to be able to function. The third argument (which can be in any position) is an optional "-v". If this is found, verbose output will be on and the program will output all of the common substrings as well as the longest.

Licence

I don't currently have a specific licence in mind at the moment. However, the conditions I want you to abide by are these: Finally, if you have done something cool with the source code, or just like my coding style or whatever, please let me know! I would love to hear from you! Contact brian {at} worthlesscheese {dot} com