Advertisement
azamat_tajiyev

14. Longest Common Prefix

Apr 24th, 2024
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.52 KB | None | 0 0
  1. class Solution {
  2.     func longestCommonPrefix(_ strs: [String]) -> String {
  3.         if strs.count == 1 {
  4.             return strs[0]
  5.         }
  6.         var pref: String = ""
  7.         let word = strs.first ?? ""
  8.         for index in word.indices {
  9.             let c = word[index]
  10.             for j in 1..<strs.count {
  11.                 if strs[j].count == pref.count || c != strs[j][index] {
  12.                     return pref
  13.                 }
  14.             }
  15.             pref.append(c)
  16.         }
  17.         return pref
  18.     }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement