We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
C++ solution: (The idea here is quite interesting to me :)) )
-If k >= n + m you can can just totally just delete the whole string s and delete the empty string how many times you want and then append letters to make it the same as string t
-But if that not the case:
+)Cnt is for counting the number of characters which are similar between s and t
+) The loop goes on until it hits the end of string s or string t(last char of string t is by '\0') or their character are different
+) So now the number of characters you need to delete from s is: s.length() - cnt and the number of characters you need to append to s is t.length() - cnt and they combine into n + m - cnt * 2 (this is at least cause you can keep deleting and rewrite the last letter in s)
+) You can easily recognize that it always take x steps to delete and rewrite once like that so x % 2 == 0 and you can also see that if tmp sastisfy it has too be also odd with k or also even with k. Ex: 10 - 4 = 6 % 2 == 0 or 9 - 5 = 4 % 2 == 0
- Hopefully this help you guys :))
string appendAndDelete(string s, string t, int k) {
int n = s.size(), m = t.size();
if(k >= n + m) return "Yes";
int cnt = 0;
for(int i = 0; i < s.length(); i++){
if(t[i] == '\0') break;
if(s[i] != t[i]) break;
else cnt++;
}
int tmp = n + m - cnt * 2;
if(tmp <= k && (k - tmp) % 2 == 0) return "Yes";
else return "No";
}
int tmp = n + m - cnt * 2;
if(tmp <= k && (k - tmp) % 2 == 0) return "Yes";
else return "No";
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Append and Delete
You are viewing a single comment's thread. Return to all comments →
C++ solution: (The idea here is quite interesting to me :)) ) -If k >= n + m you can can just totally just delete the whole string s and delete the empty string how many times you want and then append letters to make it the same as string t -But if that not the case: +)Cnt is for counting the number of characters which are similar between s and t +) The loop goes on until it hits the end of string s or string t(last char of string t is by '\0') or their character are different +) So now the number of characters you need to delete from s is: s.length() - cnt and the number of characters you need to append to s is t.length() - cnt and they combine into n + m - cnt * 2 (this is at least cause you can keep deleting and rewrite the last letter in s) +) You can easily recognize that it always take x steps to delete and rewrite once like that so x % 2 == 0 and you can also see that if tmp sastisfy it has too be also odd with k or also even with k. Ex: 10 - 4 = 6 % 2 == 0 or 9 - 5 = 4 % 2 == 0 - Hopefully this help you guys :))
string appendAndDelete(string s, string t, int k) { int n = s.size(), m = t.size(); if(k >= n + m) return "Yes"; int cnt = 0; for(int i = 0; i < s.length(); i++){ if(t[i] == '\0') break;
} int tmp = n + m - cnt * 2; if(tmp <= k && (k - tmp) % 2 == 0) return "Yes"; else return "No"; }