-
Notifications
You must be signed in to change notification settings - Fork 3
/
AppendAndDelete.java
47 lines (40 loc) · 1.6 KB
/
AppendAndDelete.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// https://www.hackerrank.com/challenges/append-and-delete/problem
package implimentation;
import java.util.Scanner;
public class AppendAndDelete {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String first = scanner.next();
String second = scanner.next();
int k = scanner.nextInt();
System.out.println(canBeModified(first, second, k) ? "Yes" : "No");
}
private static boolean canBeModified(String first, String second, int k) {
Info info = getChanges(first, second, k);
int changes = info.changes;
int commonLength = info.commonSequenceLength;
if (k < changes) {
return false;
} else if ((k - changes) % 2 == 0) {
return true;
}
return k - changes >= 2 * commonLength;
}
private static Info getChanges(String first, String second, int k) {
for (int index = 0 ; index < first.length() && index < second.length() ; index++) {
if (first.charAt(index) != second.charAt(index)) {
return new Info(first.length() + second.length() - 2 * index, index);
}
}
int commonSequenceLength = Math.min(first.length(), second.length());
return new Info(first.length() + second.length() - 2 * commonSequenceLength, commonSequenceLength);
}
private static class Info {
int changes;
int commonSequenceLength;
Info(int changes, int commonSequenceLength) {
this.changes = changes;
this.commonSequenceLength = commonSequenceLength;
}
}
}