-
Notifications
You must be signed in to change notification settings - Fork 694
/
Copy pathSolution.java
60 lines (48 loc) · 2.02 KB
/
Solution.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
48
49
50
51
52
53
54
55
56
57
58
59
60
//Problem: https://www.hackerrank.com/challenges/append-and-delete
//Java 8
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.lang.Math;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.next();
String t = in.next();
int k = in.nextInt();
String result = "ERROR";
//if s == t then we just run an equation to determine if it can be done
if(s.equals(t))
{
result = (k >= s.length()*2 || k % 2 == 0) ? "Yes" : "No";
}
else//count how many characters they share, starting from the front of the string
{
int matchingCharacters = 0;
for(int i = 0; i < Math.min(s.length(), t.length()); i++)
{
//ado
//adolol
if(s.charAt(i) != t.charAt(i))
{
break;
}
matchingCharacters++;
}
int nonMatchingCharactersInS = s.length() - matchingCharacters;
int nonMatchingCharactersInT = t.length() - matchingCharacters;
//I reassign here to make the conditions more readable down below
int nmcS = nonMatchingCharactersInS;
int nmcT = nonMatchingCharactersInT;
//use the number of non matching characters in an equation to determine if it can be done
//Naming the conditions to increase readability
boolean conditionA = nmcS + nmcT == k;
boolean conditionB = (nmcS + nmcT < k && (nmcS + nmcT - k) % 2 == 0 );
boolean conditionC = s.length() + t.length() <= k;
result = (conditionA || conditionB || conditionC) ? "Yes" : "No";
}
System.out.println(result);
}
}