29_09_2022

def isPalin(s):

    for i in range(len(s)):

        if s[i]!=s[len(s)-1-i]:

            return False

    return True

#     return [False for i in range(len(s)) if s[i]!=s[len(s)-1-i]]

def give_max(s):

    '''Gives the max len of contiguous possible palindromic substring of the given string'''

    c_len=0

    m_len=0

    for i in range(len(s)-1):

        for j in range(i+1,len(s)):

            c_len=0

            if(isPalin(s[i:j+1])):

                #Check if the string is a palindrome

                c_len=j+1-i

                if c_len>m_len:

                    m_i=i

                    m_j=j+1

                    m_len=c_len

#     return m_len,(s[m_i:m_j])

    return m_len


def give_max2(s):

    '''Givesthe max len of the possible palindromic substring of the provided string(not necesarily to be contiguous)'''

    seen_elems=[]

    empty_dict=dict()

    for elem in s:

        if elem not in seen_elems:

            seen_elems.append(elem)

            freq=0

            for i in range(len(s)):

                if s[i]==elem:

                    freq+=1

            empty_dict[elem]=freq

    even_vals=sum([i for i in empty_dict.values() if i%2==0])

    max_odd_val=max([i for i in empty_dict.values() if i%2!=0])

    print(even_vals+max_odd_val)

    print(empty_dict)

def rotate_str(s:str,k:int):

    return s[k:]+s[:k]


def main_func(s:str):

    tmp=""

    for i in range(len(s)):

        if i==0:tmp=rotate_str(s,0)

        if i!=0:tmp=rotate_str(tmp,1)

#         print("The tmp_str returned by the rotate_str func is ",tmp)

        print(give_max(tmp))

def main():

#     main_func("HimeshSing",3)

    main_func("aaaaabbbbaaaa")

if __name__=="__main__":

    main() 

Comments

Popular Posts