[Toggle Comments]
package main // file: impl-002.go // Compare characters from top to bottom, instead of lef to right. // time: O(S); space: O(1); S == sum of all chars in all words. func lcpVerticalScan(ss []string) string { if len(ss) == 0 { return "" } firstWord := ss[0] for i := 0; i < len(firstWord); i++ { for j := 1; j < len(ss); j++ { currentWord := ss[j] // Current word’s current char is out of bounds. // Return the matched prefix so far. if len(currentWord) == i { return firstWord[0:i] } // The current chars match, move on to the next word. if currentWord[i] == firstWord[i] { continue } // Current chars do not match, end the search. // Return what has already matched so far. return firstWord[0:i] } } // All of the chars of the first word is a substring of all the other words. return ss[0] } /* * \ * \\, * \\\,^,.,,. Zero to Hero * ,;7~((\))`;;,, <zerotohero.dev> * ,(@') ;)`))\;;', stay up to date, be curious: learn * ) . ),(( ))\;, * /;`,,/7),)) )) )\,, * (& )` (,((,((;( ))\, */