
    Design an algorithm for an essay analysis tool that identifies the longest symmetrical pattern of letters in a student's writing, where letters can be selected from any position while maintaining their relative order, to evaluate their mastery of advanced literary devices. The symmetrical pattern must read the same forwards and backwards when the selected letters are extracted from the text, and the algorithm should return the length of the longest such pattern found in the essay.
    

    
def lps(str):
    """
    Design an algorithm for an essay analysis tool that identifies the longest symmetrical pattern of letters in a student's writing, where letters can be selected from any position while maintaining their relative order, to evaluate their mastery of advanced literary devices. The symmetrical pattern must read the same forwards and backwards when the selected letters are extracted from the text, and the algorithm should return the length of the longest such pattern found in the essay.
    

    """
def lps(str):
    """
    Design an algorithm for an essay analysis tool that identifies the longest symmetrical pattern of letters in a student's writing, where letters can be selected from any position while maintaining their relative order, to evaluate their mastery of advanced literary devices. The symmetrical pattern must read the same forwards and backwards when the selected letters are extracted from the text, and the algorithm should return the length of the longest such pattern found in the essay.
    

    """
    n = len(str)
    L = [[0 for x in range(n)] for x in range(n)]
    for i in range(n):
        L[i][i] = 1
    for cl in range(2, n + 1):
        for i in range(n - cl + 1):
            j = i + cl - 1
            if str[i] == str[j] and cl == 2:
                L[i][j] = 2
            elif str[i] == str[j]:
                L[i][j] = L[i + 1][j - 1] + 2
            else:
                L[i][j] = max(L[i][j - 1], L[i + 1][j])
    return L[0][n - 1]