2nd case: least diff < k, but k – least_dff is divisible by 2, we can exhaustively append one letter and then remove one after achieving the final state of 1st case
1st + 2nd case: least_diff <= k && (k – least_diff) is divisible by 2
3rd case: k is no smaller than the sum of the length of the 2 strings, we can remove a whole string and construct an identical string to the other. When the string is empty, we can use up the extra steps by trying to remove a letter from the empty string.
#include <bits/stdc++.h>
using namespace std;
// Complete the appendAndDelete function below.
string appendAndDelete(string s, string t, int k) {
int i;
bool isDiff = true;
for(i = 0; i != s.size() && i != t.size() ; i++) {
if(s[i] != t[i]) {
break;
}
}
int d = s.size() + t.size() - 2 * i;
if(k >= d && (d - k) % 2 == 0) return "Yes";
if(k > s.size() + t.size()) return "Yes";
return "No";
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
string s;
getline(cin, s);
string t;
getline(cin, t);
int k;
cin >> k;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
string result = appendAndDelete(s, t, k);
fout << result << "\n";
fout.close();
return 0;
}