ARTICLE AD BOX
While solving a question in LeetCode (Longest common prefix), I used the startsWith and slice operation of JavaScript both of which are o(N) operations. But LeetCode claims the solution I submitted is o(N) and is pretty fast, which I know is not true. Can anyone tell me how the time complexity can be calculated exactly for this problem? The solution I used is given below:
/** * @param {string[]} strs * @return {string} */ var longestCommonPrefix = function(strs) { if(strs.length == 0 ){ return "" } if(strs.length == 1 ){ return strs[0] } let substring = strs[0] ; for(let i = 1 ; i < strs.length ; i++) { while(!strs[i].startsWith(substring)){ if(substring == "") { return substring } else { substring = substring.slice(0,-1) } } } return substring };