From 4bb51079549c69597ebb8ffff0fb76639a20ef7e Mon Sep 17 00:00:00 2001 From: Rajdeep Chakraborty <68934988+rajdeepchakraborty-rc@users.noreply.github.com> Date: Sun, 10 Nov 2024 12:57:34 +0530 Subject: [PATCH] Added: Longest Common Prefix Program Added the C code and the README file for the program --- .../Longest_Common_Prefix/README.md | 91 +++++++++++++++++++ .../longest_common_prefix.c | 62 +++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 String Algorithms/Longest_Common_Prefix/README.md create mode 100644 String Algorithms/Longest_Common_Prefix/longest_common_prefix.c diff --git a/String Algorithms/Longest_Common_Prefix/README.md b/String Algorithms/Longest_Common_Prefix/README.md new file mode 100644 index 00000000..e965b758 --- /dev/null +++ b/String Algorithms/Longest_Common_Prefix/README.md @@ -0,0 +1,91 @@ +# Longest Common Prefix + +## Description + +This program finds the Longest Common Prefix (LCP) among an array of strings. The LCP is the longest sequence of characters shared at the beginning of all strings in the array. The program employs an efficient algorithm to identify the common prefix by comparing the strings character by character. + +``` +Example: +Enter the number of strings: 5 +Enter the strings: +String 1: prawn +String 2: prajakta +String 3: prince +String 4: probably +String 5: project + +Output: +The longest common prefix is: pr +``` + +### Problem Definition + +1. **Given**: +- An array of strings and the number of strings, `n`. + +2. **Objective**: +- Find the longest sequence of characters common to the beginning of all strings in the array. + +### Algorithm Overview + +1. **Input Validation**: + - Ensure the array contains at least one string. + +2. **Prefix Comparison**: + - Start with the first string as the initial prefix. + - Compare the prefix with each subsequent string character by character. + - Update the prefix to the common portion after each comparison. + +3. **Early Termination**: + - If the prefix becomes empty at any point, terminate early as there is no common prefix. + +5. **Output Result**: + - Display the longest common prefix if found; otherwise, indicate no common prefix. + +### Key Features + +- Efficiently determines the common prefix without unnecessary comparisons. +- Handles edge cases such as identical strings, empty strings, and no common prefix. +- User-friendly input and output. + +### Time Complexity + +- **Best case**: `O(n * m)`, where `n` is the number of strings and `m` is the length of the shortest string. +- **Worst case**: Same as the best case since each character is processed at most once. + +### Space Complexity + +- `O(m)` for storing the prefix, where `m` is the length of the shortest string + +## Implementation + +The implementation in C includes: + +1. **Input Handling**: + - Accepts the number of strings and their contents. + +2. **Logic**: + - A function to iteratively compute the longest common prefix. + - Compares the prefix with each string and updates it to the common characters. + +## Edge Cases for Testing + +1. **No Common Prefix**: + - Input: `["dog", "racecar", "car"]` + - Output: `There is no common prefix among the strings.` +2. **All Strings Identical**: + - Input: `["apple", "apple", "apple"]` + - Output: `The longest common prefix is: apple` +3. **Single String**: + - Input: `["alone"]` + - Output: `The longest common prefix is: alone` +4. **Empty Strings**: + - Input: `["", "abc", "abcd"]` + - Output: `There is no common prefix among the strings.` + +## Usage + +- Compile the program using a C compiler (e.g., `gcc longest_common_prefix.c -o lcp`). +- Run the program (`./lcp`). +- Input the number of strings and their values as prompted. +- Observe the output indicating the longest common prefix or a message stating there is no common prefix. \ No newline at end of file diff --git a/String Algorithms/Longest_Common_Prefix/longest_common_prefix.c b/String Algorithms/Longest_Common_Prefix/longest_common_prefix.c new file mode 100644 index 00000000..7f8b1ccf --- /dev/null +++ b/String Algorithms/Longest_Common_Prefix/longest_common_prefix.c @@ -0,0 +1,62 @@ +#include +#include + +// Defined max value for number of strings and their length +#define MAX_STRINGS 100 +#define MAX_LENGTH 100 + +// Function to find the longest common prefix among an array of strings +char* longestCommonPrefix(char arr[][MAX_LENGTH], int n) { + static char prefix[MAX_LENGTH]; + strcpy(prefix, arr[0]); // Assume the first string as the prefix + + for (int i = 1; i < n; i++) { + int j = 0; + + // Compare the current prefix with the next string character by character + while (j < strlen(prefix) && j < strlen(arr[i]) && prefix[j] == arr[i][j]) { + j++; + } + + // Update the prefix to the common portion + prefix[j] = '\0'; + + // If the prefix becomes empty, return immediately + if (prefix[0] == '\0') { + return prefix; + } + } + + return prefix; +} + +int main() { + int n; + + printf("Enter the number of strings: "); + scanf("%d", &n); + + if (n <= 0 || n > MAX_STRINGS) { + printf("Invalid number of strings! Please enter a value between 1 and %d.\n", MAX_STRINGS); + return 1; + } + + // 2D array with rows as strings and columns for string lenth for easy comparison + char arr[MAX_STRINGS][MAX_LENGTH]; + + printf("Enter the strings:\n"); + for (int i = 0; i < n; i++) { + printf("String %d: ", i + 1); + scanf("%s", arr[i]); + } + + char* prefix = longestCommonPrefix(arr, n); + + if (strlen(prefix) > 0) { + printf("The longest common prefix is: %s\n", prefix); + } else { + printf("There is no common prefix among the strings.\n"); + } + + return 0; +}