File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed
problems/leet-code/top-150-interview/string Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution :
2+ def longestCommonPrefix (self , strs : list [str ]) -> str :
3+ """
4+ first of all I sorted the list and then compare the first element
5+ with the last element together, then `answer` save the same characters.
6+
7+ Runtime: 0ms | Beats: 100.00%
8+ """
9+
10+ answer = ""
11+ sorted_strs = sorted (strs ) # sort the list by ascii
12+ first = sorted_strs [0 ]
13+ last = sorted_strs [- 1 ]
14+
15+ for char in range (0 , len (first )):
16+ if first [char ] != last [char ]:
17+ break
18+ else :
19+ answer += first [char ]
20+ return answer
21+
22+
23+ problem_link = 'https://leetcode.com/problems/longest-common-prefix/submissions/1434530918/?envType=study-plan-v2&envId=top-interview-150'
24+
You can’t perform that action at this time.
0 commit comments