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.
My solution in Java language, some variables' name may look weird.
publicstaticStringappendAndDelete(Strings,Stringt,intk){// Write your code here/* * Idea: - Append and remove are 1 operation. - Convert is remove and then append -> 2 operation. - 2 string may have different length or not. - If same length, no more add or remove, just convert (basically remove and add) - If A is the difference in length, then do A operation to add or delete. - Step: + Check difference in length + If positive, find the left-most difference character, then convert from that character to the end. + The following steps are below - Time complexity: O(min(s.length(), t.length())) */intsteps=Math.abs(s.length()-t.length());intminL=Math.min(s.length(),t.length());intdiff=minL;for(inti=0;i<minL&&s.charAt(i)==t.charAt(i);i++){diff--;}//Find the minimum steps to convert string s to string t.steps+=diff*2;//If k - steps = 2*h with h > 0, we can repeatedly delete and addif((steps-k)%2==0&&steps<=k)return"Yes";//Else after deleted all necessary character, check if it is possible to //delete all the remaining character and empty string, add it again and fit//the k.return(steps+2*(minL-diff)<=k)?"Yes":"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 →
My solution in Java language, some variables' name may look weird.