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:

  • S will 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的更多相关文章

  1. [LeetCode] 767. Reorganize String 重构字符串

    Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...

  2. [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 就是给你一个字符串,能不 ...

  3. 767. Reorganize String - LeetCode

    Question 767. Reorganize String Solution 题目大意: 给一个字符串,将字符按如下规则排序,相邻两个字符一同,如果相同返回空串否则返回排序后的串. 思路: 首先找 ...

  4. 767. Reorganize String

    Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...

  5. 【LeetCode】767. Reorganize String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/reorganiz ...

  6. 【leetcode】Reorganize String

    题目如下: Given a string S, check if the letters can be rearranged so that two characters that are adjac ...

  7. [LC] 767. Reorganize String

    Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...

  8. LeetCode 358. Rearrange String k Distance Apart

    原题链接在这里:https://leetcode.com/problems/rearrange-string-k-distance-apart/description/ 题目: Given a non ...

  9. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

随机推荐

  1. Oracle分组小计、总计示例(grouping sets的使用)

    1.首先创建一个表 create table TE ( ID        VARCHAR2(2), T_CODE    VARCHAR2(4), T_NAME    VARCHAR2(4), T_A ...

  2. OpenXC : Any updates on plans for IOS?

    OpenXC : Any updates on plans for IOS? Hi Thomas, We're actively investigating this as we'd love to ...

  3. Using async-await on .net 4

    I'm currently starting to create an application that would profit a lot from C# 5's async-await feat ...

  4. javacript onclick事件中传递对象参数

    var user = {id:1, name:'zs', age:20}; var ele = '<a onclick="edit(' + JSON.stringify(user).r ...

  5. 卷积的三种模式:full, same, valid

    通常用外部api进行卷积的时候,会面临mode选择. 本文清晰展示三种模式的不同之处,其实这三种不同模式是对卷积核移动范围的不同限制. 设 image的大小是7x7,filter的大小是3x3 1,f ...

  6. 解决Android Studio出现Failed to open zip file. Gradle's dependency cache may be corrupt的问题

    问题如下图所示: 解决: 修改 gradle-wrapper.properties里的gradle的版本,与之前没有报错的gradle版本一致.就可以了 比如我报这个错的时候 : distributi ...

  7. 单片机 MCU 中 stack 使用的探讨

    stack 的使用,是单片机开发中影响最大,但是最少被讨论的问题.而提及这个问题的地方,都是对这个问题含糊其辞. 今天花了点时间,使用最笨的办法,直接阅读汇编代码,来对这个问题就行探究,这里做一下记录 ...

  8. Java 基础【19】代理

    Java 代理(Proxy)模式与现实中的代理含义一致,如旅游代理.明星的经纪人. 在目标对象实现基础上,增加额外的功能操作,由此来扩展目标对象的功能. JavaWeb 中最常见的过滤器.Struts ...

  9. 每天一个linux命令(14):head命令

    1.命令简介 head (head) 用来显示档案的开头至标准输出中.如果指定了多于一个文件,在每一段输出前会给出文件名作为文件头.如果不指定文件,或者文件为"-",则从标准输入读 ...

  10. UESTC 1034 AC Milan VS Juventus 分情况讨论

    AC Milan VS Juventus Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Oth ...