Skip to content

Commit c98ce40

Browse files
committed
implement -Solved the problem in the LeetCode in strings with sort way.
1 parent e1ff6b4 commit c98ce40

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+

0 commit comments

Comments
 (0)