From fa5550c56a06d068142486f1ab32636a346ce5c8 Mon Sep 17 00:00:00 2001 From: chobobdev Date: Wed, 1 Sep 2021 22:41:23 +0900 Subject: [PATCH] 14. Longest Common Prefix --- chobobdev/14.go | 43 ++++++++++++++++++++++++++++++++++++++ chobobdev/{26 => }/26.go | 0 chobobdev/{27 => }/27.go | 0 chobobdev/{58 => }/58.go | 0 chobobdev/{69 => }/69.go | 0 chobobdev/{7 => }/7.go | 0 chobobdev/{796 => }/796.go | 0 chobobdev/{9 => }/9.go | 0 chobobdev/README.md | 14 ++++++------- 9 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 chobobdev/14.go rename chobobdev/{26 => }/26.go (100%) rename chobobdev/{27 => }/27.go (100%) rename chobobdev/{58 => }/58.go (100%) rename chobobdev/{69 => }/69.go (100%) rename chobobdev/{7 => }/7.go (100%) rename chobobdev/{796 => }/796.go (100%) rename chobobdev/{9 => }/9.go (100%) diff --git a/chobobdev/14.go b/chobobdev/14.go new file mode 100644 index 0000000..78d4805 --- /dev/null +++ b/chobobdev/14.go @@ -0,0 +1,43 @@ +// 14. Longest Common Prefix +// Write a function to find the longest common prefix string amongst an array of strings. + +// If there is no common prefix, return an empty string "". + +// Example 1: + +// Input: strs = ["flower","flow","flight"] +// Output: "fl" +// Example 2: + +// Input: strs = ["dog","racecar","car"] +// Output: "" +// Explanation: There is no common prefix among the input strings. + +// Constraints: + +// 1 <= strs.length <= 200 +// 0 <= strs[i].length <= 200 +// strs[i] consists of only lower-case English letters. + +func longestCommonPrefix(strs []string) string { + if len(strs) == 0 { + return "" + } + if len(strs) == 1 { + return strs[0] + } + var CommonPrefix string = "" + for i := 0; ; i++ { + for j := 0; j <= len(strs)-2; j++ { + if len(strs[j]) < i || len(strs[j+1]) < i || strs[j][:i] != strs[j+1][:i] { + return CommonPrefix + } + } + CommonPrefix = strs[0][:i] + } + return CommonPrefix + +} + +// Runtime: 4 ms, faster than 16.53% of Go online submissions for Longest Common Prefix. +// Memory Usage: 2.7 MB, less than 11.79% of Go online submissions for Longest Common Prefix. diff --git a/chobobdev/26/26.go b/chobobdev/26.go similarity index 100% rename from chobobdev/26/26.go rename to chobobdev/26.go diff --git a/chobobdev/27/27.go b/chobobdev/27.go similarity index 100% rename from chobobdev/27/27.go rename to chobobdev/27.go diff --git a/chobobdev/58/58.go b/chobobdev/58.go similarity index 100% rename from chobobdev/58/58.go rename to chobobdev/58.go diff --git a/chobobdev/69/69.go b/chobobdev/69.go similarity index 100% rename from chobobdev/69/69.go rename to chobobdev/69.go diff --git a/chobobdev/7/7.go b/chobobdev/7.go similarity index 100% rename from chobobdev/7/7.go rename to chobobdev/7.go diff --git a/chobobdev/796/796.go b/chobobdev/796.go similarity index 100% rename from chobobdev/796/796.go rename to chobobdev/796.go diff --git a/chobobdev/9/9.go b/chobobdev/9.go similarity index 100% rename from chobobdev/9/9.go rename to chobobdev/9.go diff --git a/chobobdev/README.md b/chobobdev/README.md index d04fd25..7d61f59 100644 --- a/chobobdev/README.md +++ b/chobobdev/README.md @@ -21,13 +21,13 @@ Go lang을 활용해 많은 알고리즘 해결 | # | 시도 날짜 | 문제 이름 | 언어 | 링크 | | :--: | :--------: | :-------: | :--: |:--------------------------: | | 1 | 2021-05-20 | 27. Remove Element| Go |[:link:](27.go) | -| 2 | 2021-05-23 | 58 . Length of the last word | Go |[:link:](58.md)| -| 3 | 2021-05-24 | 7. Reverse Integer | Go,JS |[:link:](7.md)| -| 5 | 2021-05-20 | 26.Remove Duplicates from Sorted Array| Go |[:link:](26.md) | -| 6 | 2021-05-29 | 9. Palindrome Number | Go | [:link:](9.md) | -| 7 | 2021-05-31 |69. Sqrt(x) | Go | [:link:](69.md) | -| 8 | 2021-05-31 |796. Rotate String | Go | [:link:](796.md) | - +| 2 | 2021-05-23 | 58 . Length of the last word | Go |[:link:](58.go)| +| 3 | 2021-05-24 | 7. Reverse Integer | Go,JS |[:link:](7.go)| +| 5 | 2021-05-20 | 26.Remove Duplicates from Sorted Array| Go |[:link:](26.go) | +| 6 | 2021-05-29 | 9. Palindrome Number | Go | [:link:](9.go) | +| 7 | 2021-05-31 |69. Sqrt(x) | Go | [:link:](69.go) | +| 8 | 2021-05-31 |796. Rotate String | Go | [:link:](796.go) | +| 9 | 2021-09-01 |14. Longest Common Prefix | Go | [:link:](14.go) |