10 examples of 'python find longest string in list' in Python

Every line of 'python find longest string in list' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
10def find_longest_sub_list(list_of_lists):
11 """
12 Find the list having largest length
13 """
14 largest = []
15 if list_of_lists:
16 largest = list_of_lists[0]
17 for list in list_of_lists[1:]:
18 if len(list) > len(largest):
19 largest = list
20
21 return largest
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

9def length_of_longest_substring(s):
10
11 if len(s) < 2:
12 return len(s)
13
14 last_seen_cache = [-1]*256
15 start = longest = 0
16 for i, char in enumerate(s):
17 pos = ord(char)
18 last_seen = last_seen_cache[pos]
19 if last_seen >= start:
20 longest = max(longest, i-start)
21 start = last_seen + 1
22 last_seen_cache[pos] = i
23
24 return max(longest, (i-start)+1)
69def longest(things: list):
70 lengths = [len(thing) for thing in things]
71 return sorted(lengths)[-1] if lengths else 0
8def longestCommonPrefix(self, strs):
9 if not strs: return ""
10 l = min(map(len, strs))
11 i = 0
12 while i < l:
13 char = strs[0][i]
14 for s in strs:
15 if s[i] != char:
16 return strs[0][:i]
17
18 i += 1
19
20 return strs[0][:i]
45def longest_common_substring(str1, str2):
46 cols = len(str1) + 1 # Add 1 to represent 0 valued col for DP
47 rows = len(str2) + 1 # Add 1 to represent 0 valued row for DP
48
49 T = [[0 for _ in range(cols)] for _ in range(rows)]
50
51 max_length = 0
52
53 for i in range(1, rows):
54 for j in range(1, cols):
55 if str2[i - 1] == str1[j - 1]:
56 T[i][j] = T[i - 1][j - 1] + 1
57 max_length = max(max_length, T[i][j])
58
59 return max_length
224def test_should_return_one_when_list_contains_string_with_single_character(self):
225 self.assertEqual(1, length_of_longest_string(['a']))
2def lengthOfLongestSubstring(self, s):
3 result = 0
4 left = 0
5 last = {}
6 for i in range(len(s)):
7 if s[i] in last and left <= last[s[i]]:
8 left = last[s[i]] + 1
9 last[s[i]] = i
10 result = max(result, i - left + 1)
11 return result
2def longestCommonPrefix(self, strs):
3 """
4 :type strs: List[str]
5 :rtype: str
6 """
7 if len(strs) == 0:
8 return ""
9 i = 0
10 j = 0
11 end = 0
12 while j < len(strs) and i < len(strs[j]):
13 if j == 0:
14 char = strs[j][i]
15 else:
16 if strs[j][i] != char:
17 break
18
19 if j == len(strs) - 1:
20 i += 1
21 j = 0
22 end += 1
23 else:
24 j += 1
25
26 return strs[j][:end]
57def lengthOfLongestSubstring(self, s):
58 # https://leetcode.com/articles/longest-substring-without-repeating-characters/
59 charMap = {}
60 for i in range(256):
61 charMap[i] = -1
62 ls = len(s)
63 i = max_len = 0
64 for j in range(ls):
65 # Note that when charMap[ord(s[j])] >= i, it means that there are
66 # duplicate character in current i,j. So we need to update i.
67 if charMap[ord(s[j])] >= i:
68 i = charMap[ord(s[j])] + 1
69 charMap[ord(s[j])] = j
70 max_len = max(max_len, j - i + 1)
71 return max_len
2def lengthOfLongestSubstring(self, s):
3 """
4 :type s: str
5 :rtype: int
6 """
7 start = 0
8 max_len = 0
9 used_char = {}
10 for i in range(len(s)):
11
12 # start<=used_char[s[i]]表示的是新出现的
13 # 而不是以前用过的
14
15 if s[i] in used_char and start <=used_char[s[i]]:
16 start = used_char[s[i]]+1
17 max_len = max(max_len, i-start+1)
18 used_char[s[i]] = i
19
20 return max_len

Related snippets