Commit ac63d1e
committed
14. Longest Common Prefix
```Solution.c char* longestCommonPrefix(char** strs, int strsSize) { if(strsSize==0) return ""; char* prefix=strs[0]; for(int i=1;i<strsSize;i++){ int j=0; while(prefix[j] && strs[i][j] && prefix[j]==strs[i][j]){ j++; } prefix[j]='\0'; } return prefix; } ``` ```Solution.cpp class Solution { public: string longestCommonPrefix(vector<string>& strs) { if (strs.empty()) return ""; string prefix = strs[0]; for (int i = 1; i < strs.size(); ++i) { while (strs[i].find(prefix) != 0) prefix = prefix.substr(0, prefix.length() - 1); if (prefix.empty()) return ""; } return prefix; } }; ``` ```Solution.cs public class Solution { public string LongestCommonPrefix(string[] strs) { if (strs.Length == 0) return ""; string prefix = strs[0]; for (int i = 1; i < strs.Length; i++) { while (!strs[i].StartsWith(prefix)) { prefix = prefix.Substring(0, prefix.Length - 1); if (prefix == "") return ""; } } return prefix; } } ``` ```Solution.dart class Solution { String longestCommonPrefix(List<String> strs) { if (strs.isEmpty) return ""; String prefix = strs[0]; for (var str in strs.skip(1)) { while (!str.startsWith(prefix)) { prefix = prefix.substring(0, prefix.length - 1); if (prefix.isEmpty) return ""; } } return prefix; } } ``` ```Solution.erl -spec longest_common_prefix(Strs :: [unicode:unicode_binary()]) -> unicode:unicode_binary(). longest_common_prefix([]) -> <<>>; longest_common_prefix([Prefix | Rest]) -> longest_common_prefix(Rest, Prefix). longest_common_prefix([], Prefix) -> Prefix; longest_common_prefix([H | T], Prefix) -> longest_common_prefix(T, common_prefix(Prefix, H)). common_prefix(A, B) -> common_prefix(A, B, <<>>). common_prefix(<<>>, _, Acc) -> Acc; common_prefix(_, <<>>, Acc) -> Acc; common_prefix(<<H1:8, T1/binary>>, <<H2:8, T2/binary>>, Acc) when H1 =:= H2 -> common_prefix(T1, T2, <<Acc/binary, H1>>); common_prefix(_, _, Acc) -> Acc. ``` ```Solution.ex defmodule Solution do @SPEC longest_common_prefix(strs :: [String.t]) :: String.t def longest_common_prefix([]), do: "" def longest_common_prefix([head | tail]) do Enum.reduce(tail, head, fn str, acc -> common_prefix(acc, str) end) end defp common_prefix(str1, str2) do str1 |> String.graphemes() |> Enum.zip(String.graphemes(str2)) |> Enum.take_while(fn {c1, c2} -> c1 == c2 end) |> Enum.map(fn {c, _} -> c end) |> Enum.join() end end ``` ```Solution.go func longestCommonPrefix(strs []string) string { if len(strs) == 0 { return "" } prefix := strs[0] for _, s := range strs[1:] { for !strings.HasPrefix(s, prefix) { prefix = prefix[:len(prefix)-1] if prefix == "" { return "" } } } return prefix } ``` ```Solution.java class Solution { public String longestCommonPrefix(String[] strs) { if (strs.length == 0) return ""; String prefix = strs[0]; for (int i = 1; i < strs.length; i++) { while (strs[i].indexOf(prefix) != 0) { prefix = prefix.substring(0, prefix.length() - 1); if (prefix.isEmpty()) return ""; } } return prefix; } } ``` ```Solution.js /** * @param {string[]} strs * @return {string} */ var longestCommonPrefix = function(strs) { if (!strs.length) return ""; let prefix = strs[0]; for (let i = 1; i < strs.length; i++) { while (!strs[i].startsWith(prefix)) { prefix = prefix.slice(0, -1); if (!prefix) return ""; } } return prefix; }; ``` ```Solution.kt class Solution { fun longestCommonPrefix(strs: Array<String>): String { if (strs.isEmpty()) return "" var prefix = strs[0] for (str in strs.drop(1)) { while (!str.startsWith(prefix)) { prefix = prefix.dropLast(1) if (prefix.isEmpty()) return "" } } return prefix } } ``` ```Solution.php class Solution { /** * @param String[] $strs * @return String */ function longestCommonPrefix($strs) { if (empty($strs)) return ""; $prefix = $strs[0]; for ($i = 1; $i < count($strs); $i++) { while (strpos($strs[$i], $prefix) !== 0) { $prefix = substr($prefix, 0, -1); if ($prefix === "") return ""; } } return $prefix; } } ``` ```Solution.py class Solution: def longestCommonPrefix(self, strs: List[str]) -> str: if not strs: return "" prefix = strs[0] for s in strs[1:]: while not s.startswith(prefix): prefix = prefix[:-1] if not prefix: return "" return prefix ``` ```Solution.rb # @param {String[]} strs # @return {String} def longest_common_prefix(strs) return "" if strs.empty? prefix = strs[0] strs.each do |s| while !s.start_with?(prefix) prefix = prefix[0...-1] return "" if prefix.empty? end end prefix end ``` ```Solution.rkt (require racket/string) (define (longest-common-prefix strs) (if (null? strs) "" (let loop ((prefix (car strs)) (rest (cdr strs))) (cond [(null? rest) prefix] [else (define s (car rest)) (define (common p) (if (or (string=? p "") (string-prefix? s p)) p (common (substring p 0 (- (string-length p) 1))))) (loop (common prefix) (cdr rest))])))) ``` ```Solution.rs impl Solution { pub fn longest_common_prefix(strs: Vec<String>) -> String { if strs.is_empty() { return "".to_string(); } let mut prefix = strs[0].clone(); for s in strs.iter().skip(1) { while !s.starts_with(&prefix) { prefix.pop(); if prefix.is_empty() { return "".to_string(); } } } prefix } } ``` ```Solution.scala object Solution { def longestCommonPrefix(strs: Array[String]): String = { if (strs.isEmpty) return "" var prefix = strs(0) for(i <- 1 until strs.length) { while(!strs(i).startsWith(prefix) && prefix.nonEmpty) { prefix = prefix.substring(0,prefix.length-1) } } prefix } } ``` ```Solution.swift class Solution { func longestCommonPrefix(_ strs: [String]) -> String { if strs.isEmpty { return "" } var prefix = strs[0] for str in strs[1...] { while !str.hasPrefix(prefix) { prefix = String(prefix.dropLast()) if prefix.isEmpty { return "" } } } return prefix } } ``` ```Solution.ts function longestCommonPrefix(strs: string[]): string { if (strs.length === 0) return ""; let prefix = strs[0]; for (let i = 1; i < strs.length; i++) { while (!strs[i].startsWith(prefix)) { prefix = prefix.slice(0, -1); if (!prefix) return ""; } } return prefix; } ```1 parent af22ecb commit ac63d1e
File tree
19 files changed
+270
-0
lines changed- sol/solution/0001-0100/0014
19 files changed
+270
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
0 commit comments