-
Notifications
You must be signed in to change notification settings - Fork 1
/
Alphabet.java
31 lines (26 loc) · 938 Bytes
/
Alphabet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.Scanner;
public class Alphabet {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String pat = sc.nextLine();
String alphabet = "abcdefghijklmnopqrstuvwxyz";
int lcs = llcs(pat, alphabet);
System.out.println(26-lcs);
}
public static int llcs(String pat, String alph) {
int m = pat.length();
int n = alph.length();
int lcs[][] = new int[m+1][n+1];
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (i==0 || j==0)
lcs[i][j] = 0;
else if (pat.charAt(i-1) == alph.charAt(j-1))
lcs[i][j] = 1 + lcs[i-1][j-1];
else
lcs[i][j] = Math.max(lcs[i-1][j], lcs[i][j-1]);
}
}
return lcs[m][n];
}
}