/* 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";
}
That's pretty straight forward. Here is the solution that pass all of the mentioned test case:
static String appendAndDelete(String s, String t, int k) {
if (s.equals(t))
return (k >= s.length() * 2 || k % 2 == 0) ? "Yes" : "No";
int commonlength = 0;
for (int i = 0; i < Math.min(s.length(), t.length()); i++) {
if (t.charAt(i) != s.charAt(i))
break;
commonlength++;
}
int cs = s.length() - commonlength;
int ct = t.length() - commonlength;
int tot = cs + ct;
return ((tot == k) || (tot < k && (tot - k) % 2 == 0) || (tot + (2 * commonlength) <= k)) ? "Yes" : "No";
}