Longest Common Prefix in JavaScript

Day 15 / 30 - Javascript Coding practice Problem : Longest Common Prefix Find the longest common prefix among an array of strings. If there’s no common prefix, return an empty string "". Solution: var longestCommonPrefix = function (strs) { let longestStr = strs[0] for (let k = 1; k < strs.length; k++) { let j = 0; while (j < strs[k].length && j < longestStr.length && strs[k][j] === longestStr[j]) { j++; } longestStr = longestStr.slice(0, j) if (longestStr === "") return "" } return longestStr; }; #JavaScript #DSA #CodingPractice #100DaysOfCode #ProblemSolving #FrontendDevelopment

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories