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. 11-14 dom

    1.DOM document object model (1) 节点树状图:Document>documentElement>body>tagname 2.我们常用的节点类型 元素节 ...

  2. GMA Round 1 函数求值

    传送门 函数求值 设函数$f(x)=x^{2018}+a_{2017}*x^{2017}+a_{2016}*x^{2016}+...+a_{2}*x^2+a_{1}*x+a_{0}$,其中$a_{0} ...

  3. itchat

    # -*- coding: utf-8 -*- """ Spyder Editor This is a temporary script file. "&quo ...

  4. Spark(四十四):使用Java调用spark-submit.sh(支持 --deploy-mode client和cluster两种方式)并获取applicationId

    之前也介绍过使用yarn api来submit spark任务,通过提交接口返回applicationId的用法,具体参考<Spark2.3(四十):如何使用java通过yarn api调度sp ...

  5. EditPlus 自用正则替换

    分享下自己用EditPlus 的一些些正则技巧,editplus版本v3.5.1 1.替换a标签链接地址为空 例如: 把所有的 1 <a href="..囧.."> 替 ...

  6. 虚拟主机连接FTP发送"AUTH TLS"命令后提示“无法连接到服务器”

    https://help.aliyun.com/knowledge_detail/36417.html?spm=5176.11065259.1996646101.searchclickresult.7 ...

  7. InfluxDB meta文件解析

    操作系统 : CentOS7.3.1611_x64 go语言版本:1.8.3 linux/amd64 InfluxDB版本:1.1.0 influxdb默认配置: /etc/influxdb/infl ...

  8. PHP类中self和$this的区别

    1.self代表类,$this代表对象2.能用$this的地方一定使用self,能用self的地方不一定能用$this静态的方法中不能使用$this,静态方法给类访问的. 今天在使用静态方法的时候,使 ...

  9. 洛谷P1048 采药

    题目OJ地址 https://www.luogu.org/problemnew/show/P1048 https://vijos.org/p/1104 题目描述辰辰是个天资聪颖的孩子,他的梦想是成为世 ...

  10. 使用插件扩展Docker

    http://wwwbuild.net/dockerone/241249.html   Docker吸引我的,同时也是促使其成功的一个重要方面,是其开箱即用的特性. “开箱即用”是指什么呢?简单来说, ...