Q767 重构字符串
给定一个字符串S
,检查是否能重新排布其中的字母,使得两相邻的字符不同。
若可行,输出任意可行的结果。若不可行,返回空字符串。
示例 1:
输入: S = "aab"
输出: "aba"
示例 2:
输入: S = "aaab"
输出: ""
注意:
S
只包含小写字母并且长度在[1, 500]
区间内。
class Solution {
public String reorganizeString(String S) {
if (S.length() == 1)
return S;
int[] nums = new int[26];
char[] chs = S.toCharArray();
for (char c : chs)
nums[c - 'a']++;
StringBuilder sb = new StringBuilder();
char temp = 'A';
while (sb.length() < chs.length) {
int max = 0;
for (int i = 0; i < 26; i++) {
if (i == temp - 'a')
continue;
if (nums[i] > 0)
max = nums[i] > nums[max] || max == temp - 'a' ? i : max;
}
if (temp - 'a' == max || nums[max] == 0)
return "";
nums[max]--;
sb.append((char)(max + 'a'));
temp = (char)(max + 'a');
}
return sb.toString();
}
}
Q767 重构字符串的更多相关文章
- Java实现 LeetCode 767 重构字符串(ASCII的转换)
767. 重构字符串 给定一个字符串S,检查是否能重新排布其中的字母,使得两相邻的字符不同. 若可行,输出任意可行的结果.若不可行,返回空字符串. 示例 1: 输入: S = "aab&qu ...
- [LeetCode] Reorganize String 重构字符串
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...
- [LeetCode] 767. Reorganize String 重构字符串
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...
- [Swift]LeetCode767. 重构字符串 | Reorganize String
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...
- LeetCode 767. 重构字符串
给定一个字符串S,检查是否能重新排布其中的字母,使得两相邻的字符不同. 若可行,输出任意可行的结果.若不可行,返回空字符串. 示例 1: 输入: S = "aab"输出: &quo ...
- C#LeetCode刷题-字符串
字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串 24.6% 中等 5 最长回文子串 22.4% 中等 6 Z字形变换 35.8% 中等 8 字符串转整数 (atoi) ...
- JSP面试题及答案
更新时间:2015-04-07 来源:网络 投诉删除 [看准网(Kanzhun.com)]JSP面试题频道小编搜集的范文“JSP面试题及答案”,供大家阅读参考,查看更多 ...
- 201621123040《Java程序设计》第2周学习总结
1.本周学习总结 关键词:Java中的字符串与数组 c语言基本语法的迁移 相关总结:在一周的学习过程中,我自主学习Java的基本语法,前期的相关语法与c语言的基本语法相近,也算是做到了很好的回顾:在郑 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- scala初学笔记
tips: 1.函数的定义: def addOne(m: Int): Int = m + 1 m为参数,要指定其类型,Int: 后边跟着返回值的类型,Int= 后边是函数的内部 2.匿名函数: (x: ...
- java.lang.ClassCastException: com.liuyang.annocation.UserAction cannot be cast to com.liuyang.annocation2.UserAction at com.liuyang.annocation2.App.test
java.lang.ClassCastException: com.liuyang.annocation.UserAction cannot be cast to com.liuyang.annoca ...
- zrender源码分析--初探如何画一个圆
今天是想看看使用zrender框架如何去,画一个圆,再加“circle”的文字在圆心. 然后开始代码: 如何部署代码,让zrender跑起来这边就不说了,官方例子就有写,地址是:https://git ...
- Swift & Objc 在同一个项目中的使用
在WWDC大会中发布了Swift让人眼前一亮.终于加了很多的现代编程语言该有的东西.很早年以前玩C#3.0+的时候这些差不多类似的 已经用的烂熟的东西终于一点一点的在看Swift Programmin ...
- Ubuntu : 解决更新时出现 Unable to locate package update
当用apt-get更新软件包时常出现错误提示Unable to locate package update, 尤其是在ubuntu server上,解决方法是: 先更新apt-get ...
- LightOJ 1098(均值不等式,整除分块玄学优化)
We all know that any integer number n is divisible by 1 and n. That is why these two numbers are not ...
- Windows下Mongodb安装及配置(转载)
转载(https://blog.csdn.net/liang377122210/article/details/79062681) MongoDB的安装很简单,设置好安装路径后,一直Next直到安装结 ...
- base64编码问题
最近遇到一个很奇怪的问题:post方式上传文件,因为文件不大,所以直接base64后作为参数扔给服务器.一开始好用,后来出问题了,上传的压缩包再下载后,能双击打开看到压缩包里面的文件,但是解压就报错, ...
- Windows上编译OpenImageIO
这里使用的是OpenImageIO 1.1.11,要将OIIO解压到[工作目录]/openimageio/oiio里面. 先要给OIIO打一个patch,patch文件是Blender提供的: 下载O ...
- unit vs2017基于nunit framework创建单元测试
unit vs2017基于nunit framework创建单元测试 一.简叙: 单元测试大型项目中是必备的,所以不可忽视,一个项目的成败就看是否有单元测试,对后期的扩展维护都带来了便利. 二.安装 ...