Skip to content

Commit

Permalink
[String] Update solution to Longest Common Prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Jun 19, 2018
1 parent 42aebad commit 549eb1f
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions String/LongestCommonPrefix.swift
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
/**
* Question Link: https://leetcode.com/problems/longest-common-prefix/
* Primary idea: Use the first string as the result at first, trim it while iterating the array
* Time Complexity: O(nm), Space Complexity: O(m), m stands for the length of first string
* Time Complexity: O(nm), Space Complexity: O(m), m stands for the length of longest prefix
*/

class LongestCommonPrefix {
func longestCommonPrefix(strs: [String]) -> String {
guard strs.count > 0 else {
return ""
func longestCommonPrefix(_ strs: [String]) -> String {
var longestPrefix = [Character](), index = 0

guard let firstStr = strs.first else {
return String(longestPrefix)
}

var res = [Character](strs[0].characters)

for str in strs {
var strContent = [Character](str.characters)
let firstStrChars = Array(firstStr)
let strsChars = strs.map { Array($0) }

while index < firstStr.count {

if res.count > strContent.count {
res = Array(res[0..<strContent.count])
}
longestPrefix.append(firstStrChars[index])

for i in 0..<res.count {
if res[i] != strContent[i] {
res = Array(res[0..<i])
break
for str in strsChars {
if index >= str.count {
return String(longestPrefix.dropLast())
}

if str[index] != longestPrefix[index] {
return String(longestPrefix.dropLast())
}
}

index += 1
}

return String(res)
return String(longestPrefix)
}
}

0 comments on commit 549eb1f

Please sign in to comment.