【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-十大经典排序算法
随机推荐
- 教你用Python实现简单监督学习算法
教你用Python实现简单监督学习算法 监督学习作为运用最广泛的机器学习方法,一直以来都是从数据挖掘信息的重要手段.即便是在无监督学习兴起的近日,监督学习也依旧是入门机器学习的钥匙. 这篇监督学习教程 ...
- 【Spring】的【Bean】管理(注解)【四个相同功能的注解】
[Spring]的[Bean]管理(注解)[四个相同功能的注解] 注解:代码里面特殊的标记,使用注解也可以完成一些相关的功能. 注解写法:@注解名称(属性名称=属性值) 注解使用在类.方法.属性上面 ...
- 【ABAP系列】SAP ABAP 行列转换的方法
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 行列转换的方法 ...
- 卷积神经网络应用于MNIST数据集分类
先贴代码 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = inpu ...
- 一个简单的dns服务器
options { listen-on port 53 { any; }; listen-on-v6 port 53 { any; }; directory "/srv/app/named& ...
- CentOS7配置NFS网络文件系统
NFS,是Network File System的简写,即网络文件系统.网络文件系统是FreeBSD支持的文件系统中的一种,也被称为NFS. NFS允许一个系统在网络上与他人共享目录和文件.通过使用N ...
- Quartz任务调度的测试Demo1(含有配置文件的demo)
Quartz是一个作业任务调度的框架,所在项目组中“消息推送模块”使用到此框架,于是写个demo熟悉下quart的用法: 使用Spring框架来集成Quartz的任务调度任务. 1.搭建Spring框 ...
- 【狗屁不通文章生成器】代码分析 (javaScript)
这几天在论坛上看到了一个很有意思的项目,一个生成"狗屁不通"的文章的程序.经过本人确定其的确是"狗屁不通"后,随后又好奇其实现,于是在其[GitHub]项目里( ...
- linux下occi操作oracle数据库,中文乱码的问题
转载:http://www.linuxidc.com/Linux/2008-02/11238.htm 前几日调通了OCI连接数据库的问题后,用Oracle自带的例子测试了一下,能正常读取数据(都是英文 ...
- centos7 安装redis 出现cc: command not found错误解决
安装过程 1. 下载并解压 cd /root/software wget http://download.redis.io/releases/redis-3.2.4.tar.gz tar -zxvf ...