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].
Approach #1 Sort by count:
class Solution {
public:
string reorganizeString(string S) {
int len = S.length();
vector<int> count(26, 0);
string ans = S;
for (auto s : S) count[s-'a'] += 100;
for (int i = 0; i < 26; ++i) count[i] += i;
sort(count.begin(), count.end());
int start = 1;
int mid = (len + 1) / 2;
for (int i = 0; i < 26; ++i) {
int times = count[i] / 100;
char c = 'a' + (count[i] % 100);
if (times > mid) return "";
for (int j = 0; j < times; ++j) {
if (start >= len) start = 0;
ans[start] = c;
start += 2;
}
}
//ans += '\0';
return ans;
}
};
767. Reorganize String的更多相关文章
- [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 题目大意: 给一个字符串,将字符按如下规则排序,相邻两个字符一同,如果相同返回空串否则返回排序后的串. 思路: 首先找 ...
- [LeetCode] 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
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...
- [LC] 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 adjacent to ...
- [Swift]LeetCode767. 重构字符串 | Reorganize String
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...
- LeetCode - Reorganize String
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...
随机推荐
- 网站web.cofig配置用户的权限
访问被拒绝. 说明: 访问服务此请求所需的资源时出错.服务器可能未配置为访问所请求的 URL. 错误消息 401.2.: 未经授权: 服务器配置导致登录失败.请验证您是否有权基于您提供的凭据和 Web ...
- 程序猿的量化交易之路(27)--Cointrader之PriceData价格数据(14)
转载须注明出处:http://blog.csdn.net/minimicall?viewmode=contents,http://cloudtrade.top/ PriceData:价格数据.价格数据 ...
- 从架构层面杜绝lua中使用未定义的变量
# 从架构层面杜绝lua中使用未定义的变量 标签(空格分隔): lua --- lua中有一个很坑的地方:1.就是如果一个变量拼写错误,会自动的认为你定义了一个值为nil的全局变量.2.如果在func ...
- EasyPlayer实现视频播放局部缩放、广角平移功能(类似水滴直播,快手视频)
本文转自:http://blog.csdn.net/jyt0551/article/details/56063869 视频播放局部缩放.广角平移功能 在预览图片的时候,利用手势控制图片的缩放.平移,已 ...
- java编程之JDBC
JDBC的常用类和接口 1. DriverManager类 管理数据库中的所有驱动程序,其所有的方法都是静态方法,调用时无需实例化,通过类名就可以直接调用. 2. Connec ...
- input光标位置
兼容谷歌火狐-input光标位置 input框在没有添加任何效果的情况下,输入文字后光标始终在最后的位置,谷歌||火狐效果一样 但是在给input加入点击事件后 谷歌:input框插入文字后,光标会自 ...
- Delphi里可将纯虚类实例化,还可调用非虚函数
这是与Java/C++的巨大不同.目前还没仔细想这个特征与TClass之间的联系,先记住结论再说.以后再回来修改这个帖子. unit Unit1; interface uses Windows, Me ...
- 解决shell脚本“syntax error near unexpected token `fi'”的问题。
执行shell脚本的时候,提示如下错误: 查询资料后发现: 执行: vi finddir.sh 然后,输入 :set ff 结果是: 解决方案就是,修改为unix: :set ff=unix 执行保存 ...
- [Java SE] 字符串连接
Java 支持多种字符串连接方式,总结如下: package cn.spads.tool.string; import java.text.MessageFormat; /** * <b> ...
- Windows Power Shell
Windows PowerShell 是一种命令行外壳程序和脚本环境,使命令行用户和脚本编写者可以利用 .NET Framework的强大功能. 它引入了许多非常有用的新概念,从而进一步扩展了您在 W ...