题目如下:

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].

解题思路:刚看到这个题目,我觉得有点无从下手。但是仔细考虑之后,我觉得这个题目满足一个经典的算法场景——把一个数组分成两个子数组,使得两个子数组和最接近。例如,输入的字符串S="aabc",可以被实例化成字段d = {a:2,b:1,c:1},那么三个字符出现的次数就构成了[2,1,1],把数组分成和最接近的两个子数组[2]和[1,1]。最后是生成输出的字符串,只要在两个数组中依次各取一个字符拼接即可。注意:如何两个子数组和差值大于2的话,那么是不可能组成题目要求的字符串的。

代码如下:

//动态规划思想分割子数组
var diff = function(map){
var len = map.length
var sum = 0
for(var i =0;i<len;i++){
sum += map[i].v
}
var dp = []
for(var i =0;i<=len;i++){
var list = []
for(var j = 0;j <= sum/2;j ++){
list.push(0)
}
dp.push(list)
}
for(var i =1;i<=len;i++) {
for(var j = 1;j <= sum/2;j ++) {
if(j>=map[i-1].v){
dp[i][j] = Math.max(dp[i-1][j],dp[i-1][j-map[i-1].v]+map[i-1].v);
}
else {
dp[i][j] = dp[i - 1][j];
}
}
}
//子数组和的差值大于等于2,直接返回空
if (sum - 2*dp[len][parseInt(sum / 2)] >=2){
return ""
}
var t = parseInt(sum/2)
var s1 = ""
var s2 = ""
//确定每个子数组的分配的元素
for(var i=len;i>0;i--){
if(dp[i][t] > dp[i-1][t]){ // 找到第一个接近 sum/2 的,然后与 它上面的比较,如果大于,则代表当前 i 被选中
t -= map[i-1].v;
var tmp = map[i-1].v
while (tmp-- > 0){
s1 += map[i-1].k
}
}
else{
var tmp = map[i-1].v
while (tmp-- > 0){
s2 += map[i-1].k
}
}
}
//生成返回值,依次从每个子数组中取一个元素,注意和较大的子数组排在前面
var s = ""
for(var i = 0;i<Math.min(s1.length,s2.length);i++){
if(s1.length > s2.length){
s += s1[i]
s += s2[i]
}
else{
s += s2[i]
s += s1[i]
} }
if(s1.length != s2.length){
s += (s1.length > s2.length ? s1[s1.length-1] : s2[s2.length-1])
}
return s
} var reorganizeString = function(S) {
var dic = new Array(26)
for(var i=0;i<S.length;i++){
var a = S[i].charCodeAt() - 'a'.charCodeAt()
if(dic[a] == undefined){
dic[a] = 1
}
else{
dic[a] ++
}
}
var d2 = []
for(var i=0;i<dic.length;i++){
if (dic[i] == undefined){
continue
}
var ascii = String.fromCharCode(i + 'a'.charCodeAt())
d2.push({'k':ascii,'v':dic[i]})
} d2.sort(function(a,b){
return b.v - a.v
})
return diff(d2) };

【leetcode】Reorganize String的更多相关文章

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

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

  2. 【LeetCode】8. String to Integer (atoi) 字符串转换整数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...

  3. 【LeetCode】984. String Without AAA or BBB 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字符串构造 日期 题目地址:https://leet ...

  4. 【leetcode】Scramble String

    Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...

  5. 【leetcode】 Interleaving String (hard)

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

  6. 【leetcode】 Scramble String (hard)★

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  7. 【leetcode】Interleaving String

    Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...

  8. 【LeetCode】8. String to Integer (atoi) 字符串转整数

    题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...

  9. 【leetcode】8. String to Integer (atoi)

    题目描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...

随机推荐

  1. 使用xUnits来实现单元测试

    目录 前言 单元测试 xUnit 小结 附录 前言 从开始敲代码到现在,不停地都是在喊着记得做测试,记得自测,测试人员打回来扣你money之类的,刚开始因为心疼钱(当然还是为了代码质量),就老老实实自 ...

  2. Windows客户端 Linux服务器通讯 字符编码问题

    Windows下的字符编码默认是gb2312 在Linux下需要转成utf8 才能正确的看到对应的中文编码 提供转换函数 /*------------------------------------- ...

  3. Zookeeper 假死脑裂

    该问题就是服务集群因为网络震荡导致的多主多从问题,解决方案就是设置服务切换的超时时间,但也同时会导致无法达到高可用的要求.

  4. centos v7.0配置sftp

    需求: 1.建立三个sftp帐号,admin,test1,test22.三个帐号分别在/home/sftp下拥有相应的目录3.test1和test2只能进入自己的目录,admin可以进入三个目录(ch ...

  5. 【LeetCode】122、买卖股票的最佳时机 II

    Best Time to Buy and Sell Stock II 题目等级:Easy 题目描述: Say you have an array for which the ith element i ...

  6. 爬虫五之Selenium

    Selenium 自动化测试工具,支持多种浏览器: 爬虫中主要用来解决JavaScript渲染问题. 用法详解 基本使用 声明浏览器对象 from selenium import webdriver ...

  7. mysql——多表——内连接查询

    内连接查询:可以查询两个或者两个以上的表,当两个表中存在表示相同意义的字段时,可以通过该字段来连接这两个表: 当该字段的值相等时,就查询出该记录. 前期准备两个表: ), d_id ), name ) ...

  8. locust 的几种写法及部分内容说明

    第一种 **********************************   from locust import  HttpLocust, TaskSet, task import  json ...

  9. Codeforces 1156F Card Bag(概率DP)

    设dp[i][j]表示选到了第i张牌,牌号在j之前包括j的概率,cnt[i]表示有i张牌,inv[i]表示i在mod下的逆元,那我们可以考虑转移,dp[i][j]=dp[i-1][j-1]*cnt[j ...

  10. import和from.…import…

    import和from.-import- 在讲之前我们先来讲一下怎样去下载第三方库,我们把python看作一部手机,pip就是应用管家,第三方库里面的模块就是应用管家里面的一个应用 一.import模 ...