题目如下:

Given a string s, we make queries on substrings of s.

For each query queries[i] = [left, right, k], we may rearrange the substring s[left], ..., s[right], and then choose up to k of them to replace with any lowercase English letter.

If the substring is possible to be a palindrome string after the operations above, the result of the query is true. Otherwise, the result is false.

Return an array answer[], where answer[i] is the result of the i-th query queries[i].

Note that: Each letter is counted individually for replacement so if for example s[left..right] = "aaa", and k = 2, we can only replace two of the letters.  (Also, note that the initial string s is never modified by any query.)

Example :

Input: s = "abcda", queries = [[3,3,0],[1,2,0],[0,3,1],[0,3,2],[0,4,1]]
Output: [true,false,false,true,true]
Explanation:
queries[0] : substring = "d", is palidrome.
queries[1] : substring = "bc", is not palidrome.
queries[2] : substring = "abcd", is not palidrome after replacing only 1 character.
queries[3] : substring = "abcd", could be changed to "abba" which is palidrome. Also this can be changed to "baab" first
rearrange it "bacd" then replace "cd" with "ab".
queries[4] : substring = "abcda", could be changed to "abcba" which is palidrome.

Constraints:

  • 1 <= s.length, queries.length <= 10^5
  • 0 <= queries[i][0] <= queries[i][1] < s.length
  • 0 <= queries[i][2] <= s.length
  • s only contains lowercase English letters.

解题思路:对于给定一个query = [left,right,k],很容易能求出这个区间内每个字符出现的次数,如果某个字符出现了偶数次,那说明不需要经过任何改变,这个字符就能组成回文。所以这里只需要计算有多少个字符出现的次数是奇数,假设有x个字符出现的次数为奇数,那么至少就需要经过x/2次改变,才能形成回文。这里有一种情况例外,那就是只有一个字符出现的次数为奇数,那么可以不需要做任何改变。

代码如下:

class Solution(object):
def canMakePaliQueries(self, s, queries):
"""
:type s: str
:type queries: List[List[int]]
:rtype: List[bool]
"""
grid = [[0] * len(s) for _ in range(26)]
count = [0] * 26 for i,v in enumerate(s):
for j in range(26):
grid[j][i] = grid[j][i-1]
inx = ord(v) - ord('a')
count[inx] += 1
grid[inx][i] = count[inx] res = [] for left,right,k in queries:
diff = 0
for i in range(26):
if left > 0 and (grid[i][right] - grid[i][left-1]) % 2 != 0:
diff += 1
elif left == 0 and grid[i][right] % 2 != 0:
diff += 1
if diff == 1 or diff / 2 <= k:
res.append(True)
else:
res.append(False) return res

【leetcode】1177. Can Make Palindrome from Substring的更多相关文章

  1. 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List

    9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...

  2. 【leetcode】1147. Longest Chunked Palindrome Decomposition

    题目如下: Return the largest possible k such that there exists a_1, a_2, ..., a_k such that: Each a_i is ...

  3. 【LeetCode】9、Palindrome Number(回文数)

    题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...

  4. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  5. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  6. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  7. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  8. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  9. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

随机推荐

  1. Python2.7安装&配置环境变量

    python安装版本为2.7 下载安装包,双击安装,一路按照提示进行. 安装完成后,配置环境变量. 我的电脑—属性--高级系统设置—高级—环境变量--Path--编辑(将安装路径粘贴进去),添加到安装 ...

  2. Jmeter---BeanShell 常用的 vars, get, props, put ,log用法

    BeanShell介 BeanShell是用Java写成的,一个小型的.免费的.可以下载的.嵌入式的Java源代码解释器,具有对象脚本语言特性.本篇只记录一下基本的使用.有以下五个组件: Beansh ...

  3. 【MM系列】SAP 创建工厂

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP 创建工厂   前言部分 大家 ...

  4. 问题记录 | deepin15.10重装nvidia驱动及cuda

    问题描述: nvidia-smi也有显示,显卡驱动是在的,而且nvcc显示出来的cuda版本9.0也没错,不是9.1.不知道问题所在,索性重装全部. sudo tee /proc/acpi/bbswi ...

  5. 【Qt开发】解决Qt程序在Linux下无法输入中文的办法

    解决Qt程序在Linux下无法输入中文的办法 一位网友问我如何在Linux的Qt的应用程序中输入中文,我一开始觉得不是什么问题,但是后面自己尝试了一下还真不行.不仅是Qt制作的应用程序,就连Qt Cr ...

  6. python+selenium操作cookie

    WebDriver提供了操作Cookie的相关方法,可以读取.添加和删除cookie信息. WebDriver操作cookie的方法: get_cookies(): 获得所有cookie信息. get ...

  7. 【监控笔记】【1.2】监控事件系列——SQL Trace(默认跟踪与自定义跟踪)

    目录: [1.1]概念与使用 [1.2]跟踪的基本操作 --[1.2.1]查看默认跟踪是否在运行 --[1.2.2]开启默认跟踪 --[1.2.3]关闭默认跟踪 --[1.2.4]查看跟踪文件/查看跟 ...

  8. mysql数据库问题———登录进去无法操作显示You must reset your password using ALTER USER statement before executing this statement

    linux操作mysql数据库,可以登陆进去,但是操作所有命令都显示You must reset your password using ALTER USER statement before exe ...

  9. P1622释放囚犯

    这是一道绿题,是一道让人想用贪心但却是区间DP的题目,难倒了我这个蒟蒻. 这个题其实仔细观察是类似于石子合并的!合并石子的代价便是肉的数量,求最小代价.所以我们设dp[i][j]为释放第i个到第j个所 ...

  10. PyTorch环境配置及安装

    环境配置 温馨提示:为了更好的教程体验,提供视频.阅读地址 Youtube: https://www.youtube.com/playlist?list=PLgAyVnrNJ96CqYdjZ8v9Yj ...