-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Add solution for 'Append and Delete' challenge in C
Implemented the 'appendAndDelete' function to determine whether a string transformation can be made in exactly a given number of operations. This includes logic for both optimal prefix matching and excess operations handling. Added supporting utility functions for input parsing and trimming.
Showing
1 changed file
with
191 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
#include <assert.h> | ||
#include <ctype.h> | ||
#include <limits.h> | ||
#include <math.h> | ||
#include <stdbool.h> | ||
#include <stddef.h> | ||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
char* readline(); | ||
char* ltrim(char*); | ||
char* rtrim(char*); | ||
|
||
int parse_int(char*); | ||
|
||
/* | ||
* Complete the 'appendAndDelete' function below. | ||
* | ||
* The function is expected to return a STRING. | ||
* The function accepts following parameters: | ||
* 1. STRING s | ||
* 2. STRING t | ||
* 3. INTEGER k | ||
*/ | ||
|
||
/* | ||
* To return the string from the function, you should either do static allocation or dynamic allocation | ||
* | ||
* For example, | ||
* char* return_string_using_static_allocation() { | ||
* static char s[] = "static allocation of string"; | ||
* | ||
* return s; | ||
* } | ||
* | ||
* char* return_string_using_dynamic_allocation() { | ||
* char* s = malloc(100 * sizeof(char)); | ||
* | ||
* s = "dynamic allocation of string"; | ||
* | ||
* return s; | ||
* } | ||
* | ||
*/ | ||
char* appendAndDelete(char* s, char* t, int k) { | ||
int sLen = strlen(s); | ||
int tLen = strlen(t); | ||
|
||
// Find the length of the common prefix | ||
int commonLength = 0; | ||
while (commonLength < sLen && commonLength < tLen && s[commonLength] == t[commonLength]) { | ||
commonLength++; | ||
} | ||
|
||
// Calculate the minimum operations | ||
int deletions = sLen - commonLength; // Remove remaining chars in `s` | ||
int additions = tLen - commonLength; // Add remaining chars in `t` | ||
int minOperations = deletions + additions; | ||
|
||
// Check if the transformation is possible in exactly `k` operations | ||
if (k < minOperations) { | ||
return "No"; | ||
} | ||
|
||
// Check if the excess operations are even, or if we can fully delete and rebuild | ||
if ((k - minOperations) % 2 == 0 || k >= sLen + tLen) { | ||
return "Yes"; | ||
} | ||
|
||
return "No"; | ||
} | ||
|
||
int main() | ||
{ | ||
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w"); | ||
|
||
char* s = readline(); | ||
|
||
char* t = readline(); | ||
|
||
int k = parse_int(ltrim(rtrim(readline()))); | ||
|
||
char* result = appendAndDelete(s, t, k); | ||
|
||
fprintf(fptr, "%s\n", result); | ||
|
||
fclose(fptr); | ||
|
||
return 0; | ||
} | ||
|
||
char* readline() { | ||
size_t alloc_length = 1024; | ||
size_t data_length = 0; | ||
|
||
char* data = malloc(alloc_length); | ||
|
||
while (true) { | ||
char* cursor = data + data_length; | ||
char* line = fgets(cursor, alloc_length - data_length, stdin); | ||
|
||
if (!line) { | ||
break; | ||
} | ||
|
||
data_length += strlen(cursor); | ||
|
||
if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { | ||
break; | ||
} | ||
|
||
alloc_length <<= 1; | ||
|
||
data = realloc(data, alloc_length); | ||
|
||
if (!data) { | ||
data = '\0'; | ||
|
||
break; | ||
} | ||
} | ||
|
||
if (data[data_length - 1] == '\n') { | ||
data[data_length - 1] = '\0'; | ||
|
||
data = realloc(data, data_length); | ||
|
||
if (!data) { | ||
data = '\0'; | ||
} | ||
} else { | ||
data = realloc(data, data_length + 1); | ||
|
||
if (!data) { | ||
data = '\0'; | ||
} else { | ||
data[data_length] = '\0'; | ||
} | ||
} | ||
|
||
return data; | ||
} | ||
|
||
char* ltrim(char* str) { | ||
if (!str) { | ||
return '\0'; | ||
} | ||
|
||
if (!*str) { | ||
return str; | ||
} | ||
|
||
while (*str != '\0' && isspace(*str)) { | ||
str++; | ||
} | ||
|
||
return str; | ||
} | ||
|
||
char* rtrim(char* str) { | ||
if (!str) { | ||
return '\0'; | ||
} | ||
|
||
if (!*str) { | ||
return str; | ||
} | ||
|
||
char* end = str + strlen(str) - 1; | ||
|
||
while (end >= str && isspace(*end)) { | ||
end--; | ||
} | ||
|
||
*(end + 1) = '\0'; | ||
|
||
return str; | ||
} | ||
|
||
int parse_int(char* str) { | ||
char* endptr; | ||
int value = strtol(str, &endptr, 10); | ||
|
||
if (endptr == str || *endptr != '\0') { | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
return value; | ||
} |