题目如下:

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. 双屏显示——NW.js

    1.利用w10中的双屏显示设置(扩展模式) 2.Code for second window: var gui = require('nw.gui'); gui.Screen.Init(); win ...

  2. Sublime Text 3 相关

    Sublime Text 3 相关 Sublime Text 3是款非常实用代码编辑神器,但是想要用任何一款软件,掌握一些快捷键还是很有必要的.. 将Sublime Text 3 添加到右键选项中 打 ...

  3. python学习之面向对象(三)

    6.8 类的结构细化 6.8.1 类的私有成员 类中的私有成员包括:私有类的属性,私有对象属性,私有类方法 私有静态属性 类的内部可以访问,类的外部不可以访问,派生类中不可以访问 class A: _ ...

  4. python学习之不要在列表迭代的时候进行增删操作

    注意:列表不能在for循环时使用remove方法 li = [11,22,33,44] for i in li : li.remove(i) print (li) #输出 [22, 44] ​ for ...

  5. LeetCode.1018-可被5整除的二进制数(Binary Prefix Divisible By 5)

    这是小川的第379次更新,第407篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第241题(顺位题号是1018).给定0和1的数组A,考虑N_i:从A[0]到A[i]的第 ...

  6. const char* to char*(当函数传递参数时)

    来自 https://blog.csdn.net/rongrongyaofeiqi/article/details/52442169 https://blog.csdn.net/hebbely/art ...

  7. GCC+Make 自动生成 Makefile 依赖

    目录 BASIS wildcard .PHONY 静态模式 常用自动变量 自动生成依赖(GCC) -M 参数 编写 Makefile Makefile 细节说明 其他 本文内容基于 GNU MAKE. ...

  8. 【数字图像处理】Bilateral Filters

    [数字图像处理]Bilateral Filters https://www.yuque.com/lart/idh721/bf 简单介绍 双边滤波是一种非线性的可以模糊图像并且能保留一定的边缘信息的技术 ...

  9. jinja2模板接受

    from flask import Flask,render_template app = Flask(__name__)#template_folder='templates',默认就是templa ...

  10. AES ------ 第三方库

    tiny-AES-c-master.zip 这个库可用