Files

43 lines
1.6 KiB
Java
Raw Permalink Normal View History

2017-03-10 01:10:37 -06:00
// Author: Rodney Shaghoulian
2018-07-27 16:45:56 -07:00
// Github: github.com/RodneyShag
// HackerRank: hackerrank.com/RodneyShag
2017-03-10 01:10:37 -06:00
import java.util.Scanner;
2017-04-05 18:42:35 -05:00
// Case 1: See if we can completely erase String "s" and append String "t". If we need to waste
// operations to reach "k" operations, we can do so when String "s" has no characters.
2017-03-10 01:10:37 -06:00
2017-04-05 18:42:35 -05:00
// Case 2: See if we can convert String "s" to String "t" without completely erasing String "s".
// We keep erasing charcters from String "s" until it becomes a prefix of String "t". We
// then add the characters needed to turn String "s" into String "t". If we need to waste
// operations to reach "k" operations, we can only do so in groups of 2 by doing an append
// and a delete.
2017-03-10 01:10:37 -06:00
public class Solution {
2017-09-11 09:32:17 -07:00
public static void main(String[] args) {
2017-03-10 01:10:37 -06:00
Scanner scan = new Scanner(System.in);
String s = scan.next();
String t = scan.next();
int k = scan.nextInt();
scan.close();
System.out.println(canConvert(s, t, k) ? "Yes" : "No");
}
private static boolean canConvert(String s, String t, int k) {
/* Case 1 */
if (s.length() + t.length() <= k) {
return true;
}
/* Case 2 */
int i = 0; // represents index of 1st non-matching character
for ( ; i < s.length() && i < t.length(); i++) {
if (s.charAt(i) != t.charAt(i)) {
break;
}
}
int minOperations = (s.length() - i) + (t.length() - i);
return k >= minOperations && (k - minOperations) % 2 == 0;
}
}