LeetCode - 767. Reorganize String
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to each other are not the same.
If possible, output any possible result. If not possible, return the empty string.
Example 1:
Input: S = "aab"
Output: "aba"
Example 2:
Input: S = "aaab"
Output: ""
Note:
Swill consist of lowercase letters and have length in range[1, 500].
贪心
class Solution {
public String reorganizeString(String S) {
if (S == null || S.length() <= 0)
return "";
int[] chs = new int[26];
for (int i=0; i<26; i++) {
chs[i] = 0;
}
for (int i=0; i<S.length(); i++) {
chs[S.charAt(i)-'a']++;
}
StringBuilder ret = new StringBuilder();
char curChar = findMaxChar(chs, -1);
while (curChar != '#' && curChar != '$') {
ret.append(curChar);
curChar = findMaxChar(chs, ret.charAt(ret.length()-1)-'a');
}
if (curChar == '$')
return ret.toString();
else
return "";
}
private char findMaxChar(int[] chs, int target) {
char ret = '#';
int maxCnt = 0, cnt=0;
for (int i=0; i<26; i++) {
if (chs[i] == 0)
cnt ++;
if (maxCnt < chs[i] && i!=target) {
maxCnt = chs[i];
ret = (char)(i+'a');
}
}
if (cnt == 26)
return '$';
if (ret != '#')
chs[ret-'a']--;
return ret;
}
}
LeetCode - 767. Reorganize String的更多相关文章
- [LeetCode] 767. Reorganize String 重构字符串
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...
- [leetcode]Weekly Contest 68 (767. Reorganize String&&769. Max Chunks To Make Sorted&&768. Max Chunks To Make Sorted II)
766. Toeplitz Matrix 第一题不说,贼麻瓜,好久没以比赛的状态写题,这个题浪费了快40分钟,我真是...... 767. Reorganize String 就是给你一个字符串,能不 ...
- 767. Reorganize String - LeetCode
Question 767. Reorganize String Solution 题目大意: 给一个字符串,将字符按如下规则排序,相邻两个字符一同,如果相同返回空串否则返回排序后的串. 思路: 首先找 ...
- 767. 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/reorganiz ...
- 【leetcode】Reorganize String
题目如下: Given a string S, check if the letters can be rearranged so that two characters that are adjac ...
- [LC] 767. Reorganize String
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...
- LeetCode 358. Rearrange String k Distance Apart
原题链接在这里:https://leetcode.com/problems/rearrange-string-k-distance-apart/description/ 题目: Given a non ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
随机推荐
- oracle 连接
1.简述 1) 两个表的连接,是通过将一个表中的一列或者多列同另一个表中的列链接而建立起来的.用来连接两张表的表达式组成了连接条件.当连接成功后,第二张表中的数据就同第一张表连接起来了,并形成了复合 ...
- JS_高程5.引用类型(5)Array类型的操作方法
一.操作方法 1.concat()方法 基于当前数组中的所有项创建一个新数组.具体说,是先创建当前数组的一个副本,然后将接收到的参数添加到这个副本的末尾,最后返回新构建的数组.在没有给concat() ...
- 枚举进行位运算 枚举组合z
枚举进行位运算--枚举组合 public enum MyEnum { MyEnum1 = , //0x1 MyEnum2 = << , //0x2 MyEnum3 = << , ...
- CSS魔法堂:那个被我们忽略的outline
前言 在CSS魔法堂:改变单选框颜色就这么吹毛求疵!中我们要模拟原生单选框通过Tab键获得焦点的效果,这里涉及到一个常常被忽略的属性--outline,由于之前对其印象确实有些模糊,于是本文打算对其 ...
- CSS魔法堂:一起玩透伪元素和Content属性
前言 继上篇<CSS魔法堂:稍稍深入伪类选择器>记录完伪类后,我自然而然要向伪元素伸出"魔掌"的啦^_^.本文讲讲述伪元素以及功能强大的Contet属性,让我们可以通 ...
- VS2013中Python学习笔记[基础入门]
前言 在上一节中简单的介绍了在VS2013中如何进行开发Hello World,在VS2013中进行搭建了环境http://www.cnblogs.com/aehyok/p/3986168.html. ...
- 解析 ViewTreeObserver 源码(上)
主要内容:ViewTreeObserver 是被用来注册监听视图树的观察者,在视图树发生全局改变时将收到通知.本文从 ViewTreeObserver 源码出发,带你剖析 ViewTreeObserv ...
- puttygen 命令行 id_rsa.pub 转 ppk
网上只有puttygen GUI程序的说明,我是linux下的环境,懒得切换到win下了,putty 也有linux版本的,不过可用软件会少点,且GUI会没win下完善. 其实 一条命令搞定: put ...
- javaweb中使用百度、谷歌地图进行定位
第一种 百度 直接上代码: <!DOCTYPE> <html> <head> <meta name="viewport" content ...
- (原)DropBlock A regularization method for convolutional networks
转载请注明出处: https://www.cnblogs.com/darkknightzh/p/9985027.html 论文网址: https://arxiv.org/abs/1810.12890 ...