GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — GitFun/longestCommonPrefix.js at master · kennyle37/GitFun Expand file tree/ Copy pathlongestCommonPrefix.jsMore file actions 41 lines (41 loc) · 1.14 KB / Copy pathlongestCommonPrefix.jsFile metadata and controls41 lines (41 loc) · 1.14 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 /* Strategy: Compare a running prefix with each string in turn (this is a "transitive" solution -- the max must be correct in the end after a single pass, no matter what the order of strings). Comparison done by shorten the prefix from the end until there's a match. Big O: O(n^2) at worst case (all strings are the same, so indexOf must always scan. Example of a prefix match... ============================== Comparing prefix "flair" and word "flatulence"... flair flatulence (no match, shrink the prefix) flai flatulence (no match, shrink the prefix) fla flatulence MATCH! continue with next string... Example of no prefix match... ============================== Comparing prefix "foo" and word "bar" foo bar (no match, shrink the prefix) fo bar (no match, shrink the prefix) f bar (no match, shrink the prefix) "" bar MATCH! continue with next string (prefix is completely gone...) const longestCommonPrefix = arrayOfStrings => { // initialize the "longest prefix" with the entire first string // iterate from the 2nd string onward // shorten the prefix from the end until there's a match (which is guaranteed when the string is empty) }; */ |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |