【leetcode】1177. Can Make Palindrome from Substring
题目如下:
Given a string
s, we make queries on substrings ofs.For each query
queries[i] = [left, right, k], we may rearrange the substrings[left], ..., s[right], and then choose up tokof 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 isfalse.Return an array
answer[], whereanswer[i]is the result of thei-th queryqueries[i].Note that: Each letter is counted individually for replacement so if for example
s[left..right] = "aaa", andk = 2, we can only replace two of the letters. (Also, note that the initial stringsis 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^50 <= queries[i][0] <= queries[i][1] < s.length0 <= queries[i][2] <= s.lengthsonly 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的更多相关文章
- 【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 ...
- 【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 ...
- 【LeetCode】9、Palindrome Number(回文数)
题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
随机推荐
- kaptcha Spring 整合
转自:http://my.oschina.net/CandyDesire/blog/209364 生成验证码的方式有很多,个人认为较为灵活方便的是Kaptcha ,他是基于SimpleCaptcha的 ...
- Delphi XE2 之 FireMonkey 入门(35) - 控件基础: TFmxObject: 其它
Delphi XE2 之 FireMonkey 入门(35) - 控件基础: TFmxObject: 其它 TFmxObject 增加了 TagObject.TagFloat.TagString, 算 ...
- 阶段3 1.Mybatis_05.使用Mybatis完成CRUD_8 Mybatis中的返回值深入-调整实体类属性解决增和改方法的报错
如果实体类和数据库表内的字段没有保持一致 修改了属性子弹. 重新生成getter和setter还有toString方法 测试类里面,相关set的方法就都会报错. 只需要修改为最新的set方法就可以了. ...
- Oracle 无备份情况下的恢复--控制文件/数据文件
13.3无备份恢复控制文件 没有备份恢复控制文件其实就是在nomount状态,create control创建一个新的控制文件. dba必须知道4个信息才能正确的创建:数据库名.在线日志路径及其大小. ...
- 成功安装 Visio 2016 和 Office 2016 的64位版本~~
.XML是个很 的东西!!! 折腾了一下 Visio 2016_x64 和 Office 2016_x64,功夫不负! 首先,选对配置工具很重要. 之前总是失败是因为在官网下载的配置工具是给2019 ...
- mybatis使用map传递多参数报错:A query was run and no Result Maps were found for the Mapped Statement
在使用mybatis进行多参数传递时,报错: A query was run and no Result Maps were found for the Mapped Statement 'xx.xx ...
- [转帖]CGI与ISAPI的区别(转)
CGI与ISAPI的区别(转) 不知道原始网站是哪个 博客园里面也是转帖的 https://www.cnblogs.com/eret9616/p/8515095.html 不过我还是不了解CGI和IS ...
- MySQL-快速入门(3)运算符
1.常见的运算符:算术运算符.比较运算符.逻辑运算符.位运算符. 算术运算符:+.-.*./.%(求余). 比较运算符:>.<.=.>=.<=.!=.in.between an ...
- 思维体操: HDU1287破译密码
破译密码 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- Jafka源码分析——网络架构
在kafka中.每个broker都是一个server.依照一般理解,server就是一个SocketServer,其不断接收用户的请求并进行处理.在Java中进行网络连接有两种方式一种为堵塞模式一种为 ...