File tree 1 file changed +39
-0
lines changed
1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Write a function to find the longest common prefix string amongst an array of strings.
2
+ #
3
+ # If there is no common prefix, return an empty string "".
4
+ #
5
+ #
6
+ #
7
+ # Example 1:
8
+ #
9
+ # Input: strs = ["flower","flow","flight"]
10
+ # Output: "fl"
11
+ # Example 2:
12
+ #
13
+ # Input: strs = ["dog","racecar","car"]
14
+ # Output: ""
15
+ # Explanation: There is no common prefix among the input strings.
16
+ #
17
+ #
18
+ strs = ["flower" , "flow" , "flight" ]
19
+
20
+
21
+ # strs = ["dog","racecar","car"]
22
+
23
+ def longest (strs ) -> str :
24
+ str_answer = ''
25
+ po1 = 0
26
+ lenght = len (strs )
27
+ m = min (len (s ) for s in strs )
28
+
29
+ while po1 < m :
30
+ store = ''
31
+ for i in strs :
32
+ if strs [i ][po1 ] != strs [0 ][po1 ]:
33
+ break
34
+ else :
35
+ str_answer += strs [i ][po1 ]
36
+ po1 += 1
37
+
38
+
39
+ print (longest (strs ))
You can’t perform that action at this time.
0 commit comments