打印给定字符串的回文及其回文子字符串,或者至少按字典顺序打印

时间:2018-09-25 07:58:52

标签: python-3.x

Q:打印给定字符串及其回文子字符串的回文或至少按字典顺序打印(面试问题);

A:我的答案

s = input();
a = s[::-1];
t = [];
d = [];
e = [];
if len(s) > 1:
    if s == a:
        print ("palindrome");
        print(s);
    else:
        for i in range(len(s)):
            if s[i] == a[i]:
                t.append(s[i]);
                e.extend(s[i]);
            else:
                j = ''.join(e)
                d.append(j);
                e = [];

        j = ''.join(e);
        d.append(j);
        d.sort(key=len);

        if len(d)>1:
            print ("\nshortest palindrome is");
            p =min(d,key=len);
            print(p+'\n');
            print("list of sub-strings that are palindrome = %s "%d);

        if len(t) > 1:

            print("\nlexicographically first palindrome is \n" + ''.join(t));
        else :
            u = [x.upper() for x in s]
            print(''.join(u));

0 个答案:

没有答案