Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

How to efficiently find the longest common subsequence of two strings, in Java?

/*
	This is an implementation that efficiently
	finds the longest common subsequence of 
	two strings. 

	Let n be the size of first string and 
	m the size of the second string.
	Time complexity: O(nm)
	Space complexity: O(nm)
*/
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;

public class LongestCommonSubsequence {
	public static void main(String[] args) {
		String str1 = "ZXVVYZW", str2 = "XKYKZPW";
		// Below outputs: [X, Y, Z, W]
		System.out.println(longestCommonSubsequence(str1, str2));
	}
	private static List<Character> longestCommonSubsequence(String str1, String str2) {
		if (str1.length() == 0 || str2.length() == 0)
			return new ArrayList<Character>();
		// Matrix storing lengths of longest subsequence
		// for different prefix combinations
		int[][] L = new int[str1.length() + 1][str2.length() + 1];

		List<Character> result = new ArrayList<>();
		for (int i = 0; i < L[0].length; i++) {
			L[0][i] = 0;
		}

		for (int i = 0; i < L.length; i++) {
			L[i][0] = 0;
		}
		// Solve the problem using dynamic programming
		for (int i = 1; i < L.length; i++) {
			for (int j = 1; j < L[i].length; j++) {
				// Distinguish between case where current
				// chars match up and case where they don't
				if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
					L[i][j] = 1 + L[i - 1][j - 1];
				} else {
					L[i][j] = Math.max(L[i - 1][j], L[i][j - 1]);
				}
			}
		}
		// Build the sequence
		int i = L.length - 1, j = L[0].length - 1;
		while (i >= 1 && j >= 1) {
			if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
				result.add(str1.charAt(i - 1));
				i--;
				j--;
			} else {
				if (L[i - 1][j] > L[i][j - 1]) {
					i--;
				} else {
					j--;
				}
			}
		}

		Collections.reverse(result);
		return result;

	}
}
Comment

PREVIOUS NEXT
Code Example
Java :: initialize a new class java 
Java :: how to set boolean to false if null java 
Java :: java iterate hashmap 
Java :: insert string in string java 
Java :: how to increase the size of array in java 
Java :: spring boot MVC config 
Java :: java letter alphabet index 
Java :: how to find palindrome numbers in java 
Java :: How to efficiently find the area of largest rectangle that can be formed by adjacent buildings with known heights, in Java? 
Java :: java method reference 
Java :: how to change tablayout current view position in android 
Java :: json array to list in java 
Java :: javafx rectangle border size 
Java :: display two dimension array java 
Java :: unable to access jarfile 
Java :: palindrome java 
Java :: java first character string number 
Java :: how to get internet speed in android programmatically 
Java :: java repeat method 
Java :: how to subtract localdatetime in java 
Java :: design custom button in android 
Java :: convert list of dto to map java 
Java :: sort a list with custom comparator java 
Java :: java try with resources 
Java :: import android.support.v7.app.alertdialog androidx 
Java :: convertir string a char java 
Java :: how to create a function in java 
Java :: java methods 
Java :: list of arrays java 
Java :: binary to decimal java 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =