-
[LeetCode] Longest Common Prefix알고리즘 2019. 9. 20. 01:34
문제
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: ["flower","flow","flight"] Output: "fl"
Example 2:
Input: ["dog","racecar","car"] Output: "" Explanation: There is no common prefix among the input strings.
Approach
빈 vector 걸러내고, string들 중 길이가 가장 짧은 것을 기준으로 최대 탐색치를 정한다음
스트링별로 0번 index부터 같은지 다른지 본다.
시간복잡도는 O(n) n: 전체 글자수
Code
https://github.com/chi3236/algorithm/blob/master/LeetCode_LongestCommonPrefix.cpp
'알고리즘' 카테고리의 다른 글
[LeetCode] Merge Two Sorted List (0) 2019.09.21 [LeetCode] Regular Expression Matching (0) 2019.09.21 [LeetCode] String to Int (0) 2019.09.20 [LeetCode] ZigZag Conversion (0) 2019.09.19 [LeetCode] Longest Palindromic Substring (0) 2019.09.19