Reputation: 21
/* why the two test cases are not passed by this code*/ /*the link of the problem is https://www.hackerrank.com/challenges/append-and-delete/problem */
static String appendAndDelete(String s, String t, int k) {
if (s.length() + t.length() < k)
return "Yes";
int commonlength = 0;
for (int i = 0; i < Math.min(s.length(), t.length()); i++) {
if (t.charAt(i) == s.charAt(i))
commonlength++;
else
break;
}
if ((k - s.length() - t.length() + 2 * commonlength) % 2 == 0) {
return "Yes";
}
return "No";
}
Upvotes: -3
Views: 2486
Reputation: 102
solution of the problem in golang
package main
import (
"bufio"
"fmt"
"io"
"math"
"os"
"strconv"
"strings"
)
func appendAndDelete(s string, t string, k int32) string {
// Write your code here
yes := "Yes"
no := "No"
if len(s)+len(t) <= int(k) {
return yes
}
min := math.Min(float64(len(s)), float64(len(t)))
length := 0
for i := 0; i < int(min); i++ {
if s[i] == t[i] {
length++
} else {
break
}
}
total := (len(s) - length) + (len(t) - length)
if total <= int(k) && (total-int(k))%2 == 0 {
return yes
}
return no
}
func main() {
reader := bufio.NewReaderSize(os.Stdin, 16*1024*1024)
stdout, err := os.Create(os.Getenv("OUTPUT_PATH"))
checkError(err)
defer stdout.Close()
writer := bufio.NewWriterSize(stdout, 16*1024*1024)
s := readLine(reader)
t := readLine(reader)
kTemp, err := strconv.ParseInt(strings.TrimSpace(readLine(reader)), 10, 64)
checkError(err)
k := int32(kTemp)
result := appendAndDelete(s, t, k)
fmt.Fprintf(writer, "%s\n", result)
writer.Flush()
}
func readLine(reader *bufio.Reader) string {
str, _, err := reader.ReadLine()
if err == io.EOF {
return ""
}
return strings.TrimRight(string(str), "\r\n")
}
func checkError(err error) {
if err != nil {
panic(err)
}
}
Upvotes: 0
Reputation: 1
A bit long but passes all the test cases. Runtime complexity O(N)
public static String appendAndDelete(String s, String t, int k) {
// Write your code here
int sl = s.length();
int tl = t.length();
int min_1 = 0, min_2 = 0, min_3 = 0;
int counter = 0;
String res = "No";
if (sl == tl) {
for (int i = sl - 1; i >= 0; i--) {
counter++;
if (s.charAt(i) != t.charAt(i)) {
min_1 = counter * 2;
}
}
if (min_1 == 0) {
min_1 = 2;
}
min_2 = sl * 2;
min_3 = (sl * 2) + 1;
if (min_1 % 2 == 0) {
if ((k >= min_1 && k <= min_2) && (k % 2 == 0)) {
res = "Yes";
}else if (k >= min_3) {
res = "Yes";
}
}
}else if (sl > tl) {
min_1 = sl - tl;
int dif_1 = 0;
for (int i = (tl - 1); i >= 0; i--) {
counter++;
if (s.charAt(i) != t.charAt(i)) {
dif_1 = counter * 2;
}
}
min_1 += dif_1;
min_2 = ((sl - (sl - tl)) * 2) + (sl - tl);
min_3 = ((sl - (sl - tl)) * 2 + 1) + (sl - tl);
if (min_1 % 2 == 0) {
if ((k >= min_1 && k <= min_2) && (k % 2) == 0) {
res = "Yes";
}else if (k >= min_3) {
res = "Yes";
}
}else{
if((k >= min_1 && k <= min_2) && (k % 2) == 1) {
res = "Yes";
}else if (k >= min_3) {
res = "Yes";
}
}
}else if (tl > sl) {
min_1 = tl - sl;
int dif_1 = 0;
for (int i = (sl - 1); i >= 0; i--) {
counter++;
if (s.charAt(i) != t.charAt(i)) {
dif_1 = counter * 2;
}
}
min_1 += dif_1;
min_2 = ((tl - (tl - sl)) * 2) + (tl - sl);
min_3 = ((tl - (tl - sl)) * 2 + 1) + (tl - sl);
if (min_1 % 2 == 0) {
if ((k >= min_1 && k <= min_2) && (k % 2) == 0) {
res = "Yes";
}else if (k >= min_3) {
res = "Yes";
}
}else if ((k >= min_1 && k <= min_2) && (k % 2) == 1) {
res = "Yes";
}else if (k >= min_3) {
res = "Yes";
}
}
return res;
}
Upvotes: 0
Reputation: 21
Here is my solution for the problem where all the test cases are passed.
public static string appendAndDelete(string s, string t, int k)
{
char[] sArray = s.ToCharArray();
char[] tArray = t.ToCharArray();
var commonLength = 0;
var result = "";
if (s.Length < t.Length)
{
var firstChar = s[0];
var count = t.Count(x => x == firstChar);
if (count==t.Length)
{
result = "Yes";
}
else
result = "No";
}
else if (string.Compare(s, t) == 0)
{
result="Yes";
}
else
{
for (int i = 0; i < tArray.Length; i++)
{
if (sArray[i] == tArray[i])
{
commonLength++;
}
else { break; }
}
var totalSubRequired = (s.Length - commonLength) + (t.Length - commonLength);
if (k >= totalSubRequired)
{
if (string.Compare(s, t) == 0)
{
result = "Yes";
}
else
{
var commonString = s.Substring(0, commonLength);
var attachString = t.Substring(commonLength, t.Length - commonLength);
var combineString = string.Concat(commonString, attachString);
if (string.Compare(t, combineString) == 0)
{
result = "Yes";
}
else { result = "No"; ; }
}
}
else { result = "No"; ; }
}
return result;
}
Upvotes: 0
Reputation: 21435
You need to add one more condition in your code, because below condition is not enough:
(k - s.length() - t.length() + 2 * commonlength) % 2 == 0
Try this:
int balance = k - s.length() - t.length() + 2 * commonlength;
if (balance >= 0 && (balance) % 2 == 0) {
return "Yes";
}
you need to have one more addition condition : balance >= 0
as mentioned above.
Here is a working solution that passes all test cases, added comments in code for clear understanding:
static String appendAndDelete(String s, String t, int k) {
// Check if k is greater or equal to both the lengths
if (s.length() + t.length() <= k)
return "Yes";
int commonlength = 0;
// Get the common matching character length
for (int i = 0; i < Math.min(s.length(), t.length()); i++) {
if (t.charAt(i) == s.charAt(i))
commonlength++;
else {
break;
}
}
// count how many modifications still needed
int balance = s.length() - commonlength;
balance += t.length() - commonlength;
// Check if k is greater than balance count
if (balance <= k) {
// Special case, we need to perform exactly k operations
// so if balance is odd then k should be odd, if balance is even
// then k must be even.
if ((balance - k) % 2 == 0) {
return "Yes";
}
}
return "No";
}
Upvotes: 0
Reputation: 12937
That's pretty straight forward. Here is the solution that pass all of the mentioned test case:
static String appendAndDelete(String s, String t, int k) {
if (s.equals(t))
return (k >= s.length() * 2 || k % 2 == 0) ? "Yes" : "No";
int commonlength = 0;
for (int i = 0; i < Math.min(s.length(), t.length()); i++) {
if (t.charAt(i) != s.charAt(i))
break;
commonlength++;
}
int cs = s.length() - commonlength;
int ct = t.length() - commonlength;
int tot = cs + ct;
return ((tot == k) || (tot < k && (tot - k) % 2 == 0) || (tot + (2 * commonlength) <= k)) ? "Yes" : "No";
}
Upvotes: 1