Problem
LeetCode 14: Longest Common Prefix
Solution
| |
Explaination
I know 3 possible solutions for this problem. You can solve using zip and set. if all characters at a position are same then set length is 1.
Next solution is more redimentry, We find out minimum length string, this is the max length of you prefix.Iterate upto the max prefix length, starting from zero. Compare if characters match at that position in all string.
last solution is using Trie with single-child paths. I will come back later and learn it. Hopefully
References
Also see
- Concatenation of Array
- Contains Duplicate
- Valid Anagram
- Two Sum
- Group Anagrams
- Remove Element
- Majority Element
- Design HashSet
- Design HashMap
- Sort an Array
- Sort Colors
- Top K Frequent Elements
- Encode and Decode Strings
- Range Sum Query 2D Immutable
- Product of Array Except Self
- Valid Sudoku
- Longest Consecutive Sequence
- Best Time to Buy And Sell Stock II
- Majority Element II
- Subarray Sum Equals K
- First Missing Positive