/* why the two test cases are not passed by this code*/ /*the link of the problem is https://www.hackerrank.com/challenges/append-and-delete/problem */
static String appendAndDelete(String s, String t, int k) {
if (s.length() + t.length() < k)
return "Yes";
int commonlength = 0;
for (int i = 0; i < Math.min(s.length(), t.length()); i++) {
if (t.charAt(i) == s.charAt(i))
commonlength++;
else
break;
}
if ((k - s.length() - t.length() + 2 * commonlength) % 2 == 0) {
return "Yes";
}
return "No";
}
这很简单。这是通过所有上述测试用例的解决方案:
这是一个可行的解决方案,并在代码中添加了注释,以使您容易理解: