Leetcode | 14. Longest Common Prefix | Easy | Java Solution

preview_player
Показать описание
This video has the Problem Statement, Solution Walk-through, Code, for 14. Longest Common Prefix, with a Time Complexity of O(m.n) and Space Complexity of O(1)

Time Stamps ⏱️:
0:00 - Problem Statement
0:57 - Solving the Problem
2:19 - Code [Java]
4:08 - Time & Space Complexity
4:26 - IDE Debugging
____

14. Longest Common Prefix
Leetcode Longest Common Prefix
Longest Common Prefix
Longest Common Prefix Leetcode Solution
Longest Common Prefix Java Solution
Leetcode 14
Leetcode 14 solution
Leetcode Solutions Playlist
Leetcode Java Solutions Playlist
Leetcode Easy Solutions
Developer Docs Leetcode
Longest Common Prefix Developer Docs

Hashtags:
#leetcode #leetcodesolution #java #leetcodedailychallenge
Рекомендации по теме
Комментарии
Автор

class Solution {
public String longestCommonPrefix(String[] strs) {
String prefix=strs[0];
for(String s:strs){
while(!s.startsWith(prefix)){
prefix=prefix.substring(0, prefix.length()-1);
}
}

return prefix;
}
} Easiest Solution imo

arkapravochakrabarti
Автор

Arrays.sort(strs);
String one = strs[0];
String two = strs[strs.length - 1];
int idx = 0;
while (idx < one.length() && idx < two.length()) {
if (one.charAt(idx) == two.charAt(idx)) {
idx++;
} else break;
}
return one.substring(0, idx);

T.C is O(n)

pranawkaushal
join shbcf.ru