Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 回文子串_Python_String_Algorithm_Palindrome - Fatal编程技术网

Python 回文子串

Python 回文子串,python,string,algorithm,palindrome,Python,String,Algorithm,Palindrome,给定两个字符串A和B,每个字符串由小写字母组成, 是否可以选择一些非空字符串s1和s2,其中s1是a的子字符串,s2是B的子字符串,因此s1+s2是回文字符串。这里“+”表示字符串之间的连接 例如: Case 1: A='abc' B='abc' 解决方案选择s1和s2的一种可能方式是s1=“ab”,s2=“a”,这样s1+s2即“aba”是回文 Case 2: A='a' B='b' 解决方案:没有可能的方法选择s1和s2,使得s1+s2是回文 Case 2: A='a' B='b'

给定两个字符串A和B,每个字符串由小写字母组成, 是否可以选择一些非空字符串s1和s2,其中s1是a的子字符串,s2是B的子字符串,因此s1+s2是回文字符串。这里“+”表示字符串之间的连接

例如:

Case 1:

A='abc'
B='abc'
解决方案选择s1和s2的一种可能方式是s1=“ab”,s2=“a”,这样s1+s2即“aba”是回文

Case 2:
A='a'
B='b'
解决方案:没有可能的方法选择s1和s2,使得s1+s2是回文

Case 2:
A='a'
B='b'
注: 如果可能,请打印“是”,否则请打印“否” 找到两个字符串之间的回文子字符串的算法是什么

两个字符串有一个共同的字母是必要的(也是足够的)

def test(a, b):
    return not set(a).isdisjoint(set(b))

这个程序为您发布的问题提供了相当好的结果

#palindromic substrings in Python
s1='potter'
s2='topper'
count=0
print('substring  substring    allowed \n of s1 \t   of s2\tpalindromes')
for m in range(len(s1)):
   for n in range(m+1,len(s1)+1):   # n > m
    subs1=s1[m:n]               # substring of s1
    l = n-m                     # length of subs1
    print (subs1,'.................................')
    for r in range(len(s2)-l+1):
        subs2=s2[r:r+l]
        print('\t',subs2)
        if subs1==subs2[::-1]:  # string reversal
            print ('\t\t\t',subs1+subs2)
            count=count+1
if count==0:
    print ('No')
else:
    print('Yes')
这里我基本上做的是取s1的子串。子串的长度逐渐增加,如“p”、“po”、“pot”、“pott”、“potte”、“potter”。对应于s1的每个子串,我们检查另一个字符串s2中长度相等的子串。部分输出如下所示:

substring  substring    allowed 
 of s1      of s2      palindromes
p .................................
            t
            o
            p
                         pp
            p
                         pp
            e
            r
po .................................
           to
           op
                        poop
           pp
           pe
           er
pot .................................
           top
                        pottop
           opp
           ppe
           per
 :          :              :
 :          :              :
例如,与s1中的“pot”对应,s2中的“top”、“opp”、“ppe”、“per”与“pot”长度相同。但只有“顶部”在反转时返回“罐”。因此,当“pot”与“top”连接时,它就形成了一个回文。类似地,可以从这两个单词中获得的其他回文是“pp”、“poop”、“pottop”、“oo”、“otto”、“tt”、“ee”和“rr”。
但是我的程序没有解决奇数字母回文的问题。

有一件事你需要记住:从一个字符串:单词对也有单字符单词

解释:

  • 计算字数计算函数:通过两个循环语句
  • 回文检查函数
  • 现在登录就像

    public static int getCountOfPalindromAvailable (String orignalValue){
      int count=0;
      for (int parent=0; parent< orignalValue.length(); parent++ ){
         for (int child=parent; child <orignalValue.length(); child++){ 
              System.out.println(orignalValue.substring(parent, child+1) +" 
          orignalValue");
              if (isPalindrom(orignalValue.substring(parent, child+1))){
                 System.out.println(orignalValue.substring(parent, child+1)+" 
               isPalindrom");
                 count+=1;
               }
          }
      }
      return count;
    }
    
    public static boolean isPalindrom (String orignalValue){
       return orignalValue.equals(new StringBuilder(orignalValue).reverse().toString());
    }
    
    public static int getCountOfAlindromavailable(字符串原始值){
    整数计数=0;
    对于(int parent=0;parent对于(int child=parent;child)和指向问题的链接,人们不会认为您试图在编程竞赛中作弊。后者甚至不需要,可以使用一个空字符串。