How to use 'longest palindromic substring javascript' in JavaScript

Every line of 'longest palindromic substring javascript' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
971function longestCommonPrefix(arr = []) {
972 const cnt = arr.length;
973 if (cnt === 0) return '';
974 if (cnt === 1) return arr[0];
975
976 const first = arr[0];
977 // complexity: O(m * n)
978 for (var m = 0; m < first.length; m++) {
979 const c = first[m];
980 for (var n = 1; n < cnt; n++) {
981 const entry = arr[n];
982 if (m >= entry.length || c !== entry[m]) {
983 return first.substring(0, m);
984 }
985 }
986 }
987 return first;
988}
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

42function longestCommonPrefix2(strs) {
43 const s = strs[0] || '';
44 let l = strs.length;
45 if (l <= 1) {
46 return s;
47 }
48 let min = Infinity;
49 while (l-- > 1 && min) {
50 min = Math.min(getPrefixLength2(strs[l], strs[l - 1]), min);
51 }
52 return s.substr(0, min);
53}

Related snippets