Created
September 16, 2022 12:24
-
-
Save gbvanrenswoude/43eeee3d2ac7da5ec7b05bc907dfd1a0 to your computer and use it in GitHub Desktop.
LeetCode Common Prefix Length
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Given a string, split the string into two substrings at every possible point. | |
# The rightmost substring is a suffix. The beginning of the string is the prefix. | |
# Determine the lengths of the common prefix between each suffix and the original string. | |
# Sum and return the lengths of the common prefixes. Return an array where each element i is the sum for string i. | |
# Complete the 'commonPrefix' function below. | |
# The function is expected to return an INTEGER_ARRAY. | |
# The function accepts STRING_ARRAY inputs as parameter. | |
# Let's break this problem down in several stages to make it easier to solve: | |
def commonPrefix(inputs): | |
"""Given a List of strings, returns a List of integers representing the total number of common prefixes for each string in the List | |
Args: | |
inputs (List): List of Strings | |
Returns: | |
List: List of Integers | |
""" | |
# First, a List of Strings is given in. | |
# Instantiate a List to store the total number of common prefix chars for each string, and begin a for loop to process the calculation for each String in the List | |
sums = [] | |
for input in inputs: | |
# At this point, we know nothing about the string data itself, all we know is the baseline of common prefixes for each string is 0. Instantiate this. | |
total = 0 | |
# Now, we need to calculate the lenght of the common prefix for each character in the string. | |
# So let's begin with iterating over the length of the string. Because we need the index here, we are using range() instead of enumerate() or plain for. | |
for index in range(len(input)): | |
# Since we need to compare the suffix with the original string for a common prefix, store the suffix in a variable. | |
suffix = input[index:] | |
# At this point, we do not know if the first character in the suffix and in the original string match. We only know the baseline of the number of common characters between these two strings is 0. Instantiate this. | |
common = 0 | |
# Now, we can start comparing the characters one by one. For each matching character, we can increment the common variable by 1. | |
for k in range(len(suffix)): | |
if(input[k] == suffix[k]): | |
common = common+1 | |
else: | |
break | |
# At this point, we have the number of common characters between the suffix and the original string. We can add this to the total number of common prefixes possible for the string. | |
total = total+common | |
# And logically, we need to create a new element in the List we will return. | |
sums.append(total) | |
# Finally, return the List of integers representing the total number of common prefixes for each string in the List. | |
return sums |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment