Cho hai chuỗi, kiểm tra xem chuỗi thứ nhất có thể biến thành chuỗi thứ hai bằng đúng k phép toán: thêm ký tự cuối hoặc xóa ký tự cuối.
Ví dụ
#include <bits/stdc++.h>
using namespace std;
bool if_convert(string str1, string str2, int k) {
if ((str1.length() + str2.length()) < k)
return true;
int commonLength = 0;
for (int i = 0; i < min(str1.length(), str2.length()); i++) {
if (str1[i] == str2[i]) commonLength++;
else break;
}
if ((k - str1.length() - str2.length() + 2 * commonLength) % 2 == 0)
return true;
return false;
}
int main() {
string str1 = "tutorials", str2 = "point";
int k = 5;
cout << endl;
if (if_convert(str1, str2, k))
cout << "Yes";
else
cout << "No";
return 0;
}
Kết quả
No