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 ( ...
随机推荐
- python之封装与扩展性
1.封装与扩展性 封装在于明确区分内外,使得类实现者可以修改封装内的东西而不影响外部调用的代码:而外部使用者只知道一个接口(函数),只要接口(函数)名,参数不变,使用者的代码永远无需改变.这就提供了一 ...
- python之对象(实例)
1.对象是关于类而实际存在的一个例子,即实例 #类实例化得到g1这个实例 class Garen: camp='Demacia' def __init__(self,nickname,aggressi ...
- JAVA自学笔记09
JAVA自学笔记09 1.子类的方法会把父类的同名方法覆盖(重写) 2.final: 1)可修饰类.方法.变量 2)修饰类时:此时该类变为最终类,它将无法成为父类而被继承 3)修饰方法时:该方法将无法 ...
- Wordpress添加分类
网址一般不用中文,别名方便让网址更美观,如: xxx/category/new/
- ASP.NET Core WebApi
ASP.NET Core WebApi 创建项目 使用VS新建项目,选择ASP.NET Core WebAPI即可. 此时Startup的Configure.ConfigureService方法中如下 ...
- C# CountdownEvent实现
关于CountdownEvent网上的介绍比较少,因为它是实现和使用都很简单,先看看网上的一些评论吧: CountDownEvent调用成员函数Wait()将阻塞,直至成员函数Signal() 被调用 ...
- 【UML】Java代码与UML模型相互转换方法
最近重温了一下设计模式,看到大家的博客里面都是Java代码+UML视图,UML表达整体框架,然后再秀出具体的代码,点面结合.一目了然.所以也研究了一下Java代码与UML模型相互转换方法. 一.常用的 ...
- go微服务框架go-micro深度学习(一) 整体架构介绍
产品嘴里的一个小项目,从立项到开发上线,随着时间和需求的不断激增,会越来越复杂,变成一个大项目,如果前期项目架构没设计的不好,代码会越来越臃肿,难以维护,后期的每次产品迭代上线都会牵一发而动全身.项目 ...
- $.ajax()方法详解 ajax之async属性 【原创】详细案例解剖——浅谈Redis缓存的常用5种方式(String,Hash,List,set,SetSorted )
$.ajax()方法详解 jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为Str ...
- 微信小程序--相关资料
微信小程序Demo https://github.com/zce/weapp-demojustjavac/awesomewechatweapp: 微信小程序开发资源汇总 https://gith ...