-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1847 from rajdeepchakraborty-rc/prefix
Added: Longest Common Prefix Program
- Loading branch information
Showing
2 changed files
with
153 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,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. |
62 changes: 62 additions & 0 deletions
62
String Algorithms/Longest_Common_Prefix/longest_common_prefix.c
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,62 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
// 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; | ||
} |