def computeLPSArray(pattern):
    """
    Utility function to calculate the LPS (Longest Proper Prefix that is also a Suffix) array.

    Args:
        pattern (str): pattern

    Raises:
        Exception: pattern not of type string

    Returns:
        array: the LPS array
    """
    if isinstance(pattern, str) == False:
        raise Exception("The pattern is not of type string")
    if pattern is None or pattern == "":
        raise Exception("Pattern is not defined correctly")
    
    i = 0
    j = 1
    
    # Create an empty array, filled with 0.
    # IMPORTANT: since the 1st value of the array must be 0,
    # the index j (used to fill the array) will start from 1.
    arrayLPS = [0]*len(pattern)
    
    # Fill out the array
    while j < len(pattern):
        # If the current characters match, increment i, assign
        # to array[j] the new value of i, then increment i
        if pattern[i] == pattern[j]:
            i += 1
            arrayLPS[j] = i
            j += 1
        # If the current characters do not match and i is equal to 0
        # it means that for the current character, the current prefix 
        # that is also a suffix has length 0, so we put 0 in our array
        elif i == 0:
            arrayLPS[j] = 0
            j += 1
        # If the current characters do not match but i is different 
        # from 0, we need to check the value that is in the i-1 position
        # and assign it to i, without incrementing j (because we want to
        # check whether the characters corresponding to our new i and old j
        # are a match)
        else:
            i = arrayLPS[i-1]
    return arrayLPS