Longest Common Subsequence of Vowels Only
Problem
Given two strings, find the length of the longest common subsequence which contains only vowels.
Example:
Input: input1 = "vowelpunish", input2 = "english"
Output: 2
Input: input1 = "prepinsta", input2 = "prepare"
Output: 2
Code
public class VowelLCS {
static boolean isVowel(char c) {
return "aeiouAEIOU".indexOf(c) != -1;
}
public static int vowelLCS(String s1, String s2) {
int m = s1.length(), n = s2.length();
int[][] dp = new int[m + 1][n + 1];
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (s1.charAt(i - 1) == s2.charAt(j - 1) && isVowel(s1.charAt(i - 1))) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[m][n];
}
public static void main(String[] args) {
System.out.println(vowelLCS("vowelpunish", "english"));
}
}