-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbbreviation.java
More file actions
55 lines (55 loc) · 1.16 KB
/
Abbreviation.java
File metadata and controls
55 lines (55 loc) · 1.16 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.*;
public class Abbreviation {
public static String abbreviation(String a,String b) {
int col=a.length();
int row=b.length();
boolean[][] dp=new boolean[row+1][col+1];
dp[0][0]=true;
for(int i=1;i<=col;i++) {
if(Character.isLowerCase(a.charAt(i-1))) {
dp[0][i]=dp[0][i-1];
}
else {
dp[0][i]=false;
}
}
for(int i=1;i<=row;i++) {
dp[i][0]=false;
}
for(int i=1;i<=row;i++) {
for(int j=1;j<=col;j++) {
if(j<i) {
dp[i][j]=false;
}
else if(a.charAt(j-1)==b.charAt(i-1)) {
dp[i][j]=dp[i-1][j-1];
}
else if(a.charAt(j-1)!=b.charAt(i-1) && Character.isLowerCase(a.charAt(j-1))) {
if(Character.toUpperCase(a.charAt(j-1))==b.charAt(i-1)) {
dp[i][j]=dp[i-1][j-1] || dp[i][j-1];
}
else {
dp[i][j]=dp[i][j-1];
}
}
else {
dp[i][j]=false;
}
}
}
if(dp[row][col]==true) {
return "YES";
}
else {
return "NO";
}
}
public static void main(String[] args) {
String a,b;
Scanner sc=new Scanner(System.in);
a=sc.next();
b=sc.next();
System.out.println(abbreviation(a,b));
sc.close();
}
}