Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

longest common subsequence of two strings

/*
	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 :: display in java 
Java :: java create new object 
Java :: java regex case insensitive 
Java :: multiplication program java 
Java :: Access items from the ArrayList using get() function 
Java :: java sample code 
Java :: java basic syntax 
Java :: java reverse string 
Java :: java new char array 
Java :: method reference in java 
Java :: indext of minimum element in an array in java 
Java :: jsonArray to list in java 
Java :: HttpContext.GetOwinContext().Authentication.SignOut 
Java :: android java get value from listview item 
Java :: nosuchelementexception 
Java :: java write to a file 
Java :: check if first character of a string is a number java 
Java :: javafx arabic letters 
Java :: how to get string from resource id android 
Java :: java arrayList of array to array of array 
Java :: import java.lang.Math.*; 
Java :: continue in java 
Java :: what is var in java 
Java :: centos 8 install java 16 
Java :: java.lang.IllegalArgumentException: View=DecorView@5fd145b[MainActivity] not attached to window manager 
Java :: android studio setBackground 
Java :: java arraylist with double 
Java :: class, interface, or enum expected java 
Java :: online java code fixer 
Java :: Hash map, Hash set and Hash Table difference 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =