Compare all possible prefixes

Solution Breakdown

  • Grab the first word of the dataset

    • We will be creating all possible for prefixes from this word

  • Compare the rest of the dataset and see if the first word's prefixes can be a prefix from each word in the dataset

    • If so, return the longest prefix

HOW-TO: Generating all prefixes from a string

Prefixes of the word flower:

"f"
"fl"
"flo"
"flow"
"flowe"
"flower"

Method 1: String concatenation with a loop

word = "flower"
prefix = ""
for i in range(len(word)):
    prefix += word[i]
    print(f"Current Prefix: {prefix}")

Method 2: String slicing with a loop

Pseudocode

Python Solution

Code Explanation

explanation

Connected Readings

  • Type Hinting Functions (Link)

Last updated