From aeab9e7acad5017131cc93c5e3f96c1290f635fb Mon Sep 17 00:00:00 2001 From: Luke Holman Date: Mon, 23 Dec 2024 17:49:56 -0600 Subject: [PATCH] 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. --- AppendAndDelete.c | 191 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 191 insertions(+) create mode 100644 AppendAndDelete.c diff --git a/AppendAndDelete.c b/AppendAndDelete.c new file mode 100644 index 0000000..bd16167 --- /dev/null +++ b/AppendAndDelete.c @@ -0,0 +1,191 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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; +}