Leetcode #1177 构建回文串检测

解题思路:利用哈希表的思想,观察给定字符串中某个字母的出现次数是奇数还是偶数。

P.S. 在网上上传时网站有bug,输出结果都是'true'

```python
import numpy
import math
class Solution(object):
    def canMakePaliQueries(self, s, queries):
        """
        :type s: str
        :type queries: List[List[int]]
        :rtype: List[bool]
        """
        num_q=len(queries)
        output=[]
        
        for i in range(num_q):
            
            queries_i=queries[i]
            left=queries_i[0]
            right=queries_i[1]
            k=queries_i[2]

            leng_qi=right-left+1
            num_unequal=[];

            for n in range(26):
                num_unequal.append(0)
                
            odd=0

            for j in range(left,right+1):
                
                arcs=ord(s[j])
                
                for n in range(97,123):
                    if arcs==n:
                        num_unequal[n-97]+=1

            for m in range(26):
                if num_unequal[m]%2==1:
                    odd+=1

            if (leng_qi%2)==1:
                if (odd%2==1):
                    if (odd-1)/2<=k:
                        output.append('true')
                    else:
                        output.append('false')
                else:
                    if odd/2<=k:
                        output.append('true')
                    else:
                        output.append('false')
            else:
                if (odd%2==1):
                    if (odd+1)/2<=k:
                        output.append('true')
                    else:
                        output.append('false')
                else:
                    if odd/2<=k:
                        output.append('true')
                    else:
                        output.append('false')


        return output

你可能感兴趣的:(算法,python,leetcode)