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:
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的更多相关文章
- [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 ( ...
随机推荐
- JAVA自学笔记17
JAVA自学笔记17 1.Map接口 1)概述 将键映射到值的对象,一个映射不能包含重复的键,每个键最多只能映射到一个值.可以存储键值对的元素 2)与Collection接口的不同: ①Map是双列的 ...
- [原创]浅谈Web UI自动化测试
[原创]浅谈Web UI自动化测试 Web UI自动化测试相信大家都不陌生,今天来谈谈这个,我最早接触自动化测试时大约是在2004年,2006年当时在腾讯财付通算是开始正式接触自动化测试,之所以是正式 ...
- Your project is not referencing the ".NETPortable,Version=v4.5,Profile=Profile259" framework. Add a reference to ".NETPortable,Version=v4.5,Profile=Profile259" in the "frameworks" section of your proj
i want to add nuget packages to my portable class library project , then i add a project.json to my ...
- OpenNI1.5获取华硕XtionProLive深度图和彩色图并用OpenCV显示
华硕XtionPro类似Kinect,都是体感摄像机,可捕捉深度图和彩色图. 具体參数见:http://www.asus.com.cn/Multimedia/Xtion_PRO_LIVE/specif ...
- go微服务框架go-micro深度学习-目录
go微服务框架go-micro深度学习(一) 整体架构介绍 go微服务框架go-micro深度学习(二) 入门例子 go微服务框架go-micro深度学习(三) Registry服务的注册和发现 go ...
- Java密码体系结构简介:Java Cryptography Architecture (JCA) Reference Guide
来自Java官方的文档,作备忘使用. 简介: Java平台非常强调安全性,包括语言安全,密码学,公钥基础设施,认证,安全通信和访问控制. JCA是平台的一个主要部分,包含一个“提供者”体系结构和一组用 ...
- iOS开发之资料收集
github排名:https://github.com/trending, github搜索:https://github.com/search. 此文章转自github:https://github ...
- SNF软件开发机器人-子系统-功能-功能类型(普通表改为树型表)
功能类型(普通表改为树型表) 功能类型分为普通功能和树形功能.因为资源表中不存在ParentId字段,故只能将多表的主表改为树型表. 1.效果展示: 2.使用说明: (1)当需要用一张普通的资源表显示 ...
- php获取客户端IP地址的方法
参考:https://www.cnblogs.com/rendd/p/6183094.html <?php function getip() { //strcasecmp 比较两个字符,不区分大 ...
- Linux服务器CPU使用率较低但负载较高
CPU使用率较低但负载较高 问题描述 Linux 系统没有业务程序运行,通过 top 观察,类似如下图所示,CPU 很空闲,但是 load average 却非常高,如下图所示. 处理办法 load ...