Longest Common Prefix | String Matching using Sorting and Comparison#3534
Merged
fineanmol merged 3 commits intofineanmol:masterfrom Oct 31, 2025
Vidhi006:master
Merged
Longest Common Prefix | String Matching using Sorting and Comparison#3534fineanmol merged 3 commits intofineanmol:masterfrom Vidhi006:master
fineanmol merged 3 commits intofineanmol:masterfrom
Vidhi006:master
Conversation
in this i am writing the code of longest common prefix by using string substring method
As part of Hacktoberfest 2025, this contribution promotes open-source collaboration and helps new contributors understand fundamental string algorithms in a clean, beginner-friendly way. 🖋️ Language Used Python (or replace with Java / C++ / JavaScript as per your code) 🌟 Outcome This contribution enhances algorithm repositories with a well-documented and efficient solution to the Longest Common Prefix problem — a great example for anyone learning data structures and algorithms.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The Longest Common Prefix problem is a classic string manipulation challenge that focuses on finding the longest common starting substring shared among all strings in an array.
It tests understanding of string traversal, comparison, and efficient prefix reduction techniques.
The optimal approaches often involve character-by-character comparison, sorting, or divide and conquer strategies.
-> Key Concepts
String Traversal and Comparison
Lexicographical Sorting
Prefix Reduction Logic
Time Complexity: O(n * m) (where n = number of strings, m = average string length)
Space Complexity: O(1)
-> Example
Input:
strs = ["flower", "flow", "flight"]
Output:
"fl"
Explanation:
All strings share the common prefix "fl", which is the longest one among them.
-> Algorithm Overview
If the array is empty, return an empty string.
Sort the array of strings lexicographically.
Compare the first and last strings — since sorting places the most different strings farthest apart.
Find the common prefix between these two strings character by character.
Return the resulting prefix as the Longest Common Prefix.