Skip to content

Instantly share code, notes, and snippets.

@davidsan
Created October 17, 2018 18:39
Show Gist options
  • Save davidsan/a3f893552c0f8bf1b10307a5f9255707 to your computer and use it in GitHub Desktop.
Save davidsan/a3f893552c0f8bf1b10307a5f9255707 to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class d_the_pirate_alphabet {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
d_the_pirate_alphabet_solver solver = new d_the_pirate_alphabet_solver();
solver.input = br.readLine().trim();
solver.solve();
} catch (IOException e) {
}
}
}
class d_the_pirate_alphabet_solver {
String alphabet = "aeiouybcdfghjklmnpqrstvwxz";
String input;
private Integer computeLongestCommonSubseq() {
int matrix[][] = new int[alphabet.length() + 1][input.length() + 1];
for (int i = 0; i <= alphabet.length(); i++) {
for (int j = 0; j <= input.length(); j++) {
if (i == 0 || j == 0) {
matrix[i][j] = 0;
} else {
if (alphabet.charAt(i - 1) == input.charAt(j - 1)) {
matrix[i][j] = matrix[i - 1][j - 1] + 1;
} else {
matrix[i][j] = Math.max(matrix[i - 1][j], matrix[i][j - 1]);
}
}
}
}
return matrix[alphabet.length()][input.length()];
}
public void solve() {
System.out.println(alphabet.length() - computeLongestCommonSubseq());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment