输出三行,分别表示三个字符串str1,str2和aim。,
表示字符串长度。
输出“YES”或者“NO”。(不包含引号)
AB 12 1AB2
YES
2019 9102 22001199
NO
时间复杂度,空间复杂度
。(n表示字符串str1长度,m表示s字符串tr2长度)
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s1 = sc.nextLine();
String s2 = sc.nextLine();
String s3 = sc.nextLine();
int a = s1.length(), b = s2.length(), c = s3.length();
if (a + b != c) {
System.out.print("NO");
} else if (func(s1, s2, s3)) {
System.out.print("YES");
} else {
System.out.print("NO");
}
}
public static boolean func(String s1, String s2, String s3) {
boolean[][] f = new boolean[s1.length() + 1][s2.length() + 1];
f[0][0] = true;
for (int i = 1; i <= s1.length(); i++) {
if (s1.charAt(i - 1) == s3.charAt(i - 1)) {
f[i][0] = true;
} else {
break;
}
}
for (int j = 1; j <= s2.length(); j++) {
if (s2.charAt(j - 1) == s3.charAt(j - 1)) {
f[0][j] = true;
} else {
break;
}
}
for (int i = 1; i <= s1.length(); i++) {
for (int j = 1; j <= s2.length(); j++) {
f[i][j] = (f[i -1][j] == true && s1.charAt(i - 1) == s3.charAt(i + j - 1)) || (f[i][j -1] == true && s2.charAt(j - 1) == s3.charAt(i + j - 1));
}
}
return f[s1.length()][s2.length()];
}
} java 100%