3 examples of 'smallest common substring' in Python

Every line of 'smallest common substring' 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
336def _commonprefix(strings: Iterable[str]) -> str:
337 # Similar to os.path.commonprefix
338 if not strings:
339 return ""
340
341 else:
342 s1 = min(strings)
343 s2 = max(strings)
344
345 for i, c in enumerate(s1):
346 if c != s2[i]:
347 return s1[:i]
348
349 return s1
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
93def findCommonSubstring(strings):
94 substr = strings[0]
95 for string in strings:
96 for index in range(len(string)):
97 if substr[index] != string[index]:
98 substr = string[:index]
99 break
100 return substr

Related snippets