-
Notifications
You must be signed in to change notification settings - Fork 14
Solution #14 - Daniel/Edited - 16.03.2025 #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Solution #14 - Daniel/Edited - 16.03.2025 #36
Conversation
Arrays & Strings/#14 - Longest Common Prefix - Easy/Solution - Longest Common Prefix - Easy
Outdated
Show resolved
Hide resolved
...s & Strings/#14 - Longest Common Prefix - Easy/Explanation - Longest Common Prefix - Easy.md
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kindly make the requested changes and do the same for the other PR's as well
### 1. Lexicographic Sorting | ||
|
||
1. Lexicographically sorting the array | ||
2. Comparing the first and last of the sorted array for prefix. | ||
3. Create an empty string called prefix. | ||
4. Iteratively, go through the characters to add matching characters to the prefix string. | ||
5. When there is a mismatch, end the loop and return the prefix string. | ||
|
||
- Since we are lexicographically sorting the array, the first and last string are the most different. Finding the common prefix between the two, would be the longest common prefix for all the strings in the array between them. | ||
|
||
#### Time Complexity | ||
|
||
O(nlogn) because we sort the array, traversing the two strings will be done in O(n) time which is overshadowed by the worser time complexity O(nlogn). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much better! Kindly add the solutions corresponding to the explanation as well into the cpp file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kindly add the solutions corresponding to the explanation as well into the cpp file
Solution and Explanation uploaded