[Python] Find largest sub sequence with the antakshari property [closed]

Discussão em 'Python' iniciado por Stack, Março 6, 2025.

  1. Stack

    Stack Membro Participativo

    The input is in multiple lines. The first line contains a positive integer n. This is followed by n lines, each containing sequences of words. Each line thus consists of multiple words, separated by commas, with no spaces in between words.

    You have to output, for each line, the length of the longest subsequence of words following the antakshari property.

    Assume all words are lowercase.

    A sub-sequence is a subset of consecutive words in this sequence.

    A sub-sequence is said to have the antakshari property if the last letter of every word is equal to the first letter in the next word in the sequence.

    Example


    Input:

    2
    one,two,order,real,long,tight,tree,cool,lot,trouble
    ant,tree,ear,rat,tower,retail


    Output:

    4


    The code I have tried

    def solve():
    n = int(input())
    for _ in range(n):
    words = input().split(',')
    max_length = 0
    for i in range(len(words)):
    for j in range(i,len(words)):
    sub_sequence = words[i:j+1]
    if is_antakshari(sub_sequence):
    max_lenngth = max(max_length, len(sub_sequence))
    print(max_length)
    def is_antakshari(words):
    if not words:
    return True
    for i in range(len(words)-1):
    if words[-1] == words[i+1][0]:
    return True
    return False

    solve()

    Continue reading...

Compartilhe esta Página