Q:

Given two strings, write a method that finds the longest common sub sequence

0

Given two strings, write a method that finds the longest common sub sequence.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

public String findLongestCommonSequence(String s1, String s2) {
String result = "";
for (int length = s1.length(); length > 0; length--) {
    int startIndex = 0;
    while (startIndex + length <= s1.length()) {
        String current = s1.substring(startIndex, startIndex + length);
        if (s2.contains(current)) {
            result = current;
            break;
        }
        startIndex++;
    }
    if (result.length() != 0) {
        break;
    }
}
return result;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now