算法题目解题记录——BestString

本文由作者三汪首发于简书。
历史解题记录已同步更新github.

题目

Problem Description
Given a string, you use some letters or digits to creat a new string, this new string have three properties.
1.any two adjacent letter aren't the same.
2.the new string have the biggest length.
3.the new string have the biggest lexicographical ordering.
Input
The input consists of multiple test cases. Each test case contain a string, the string is consist of 'a'~'z', 'A' - 'Z' and '0' - '9'. the length of string is not exceed 500.
Output
For each case, print the new string.
Sample Input

2
abaa
001

Sample Output

aba
010

题目分析

题意是:输入一个包含 'a'~'z', 'A' - 'Z' 和'0' - '9'的长度不大于500的字符串,要求根据输入字符串创建一个符合以下规则的新字符串:相邻字符不能相同,最大长度,字典排序的最大序列。

具体代码

public class BestString {

    public String handle(String input){
        char[] characters = input.toCharArray();
        characters = permutationWithDictionary(characters);//对数组进行字典排序获取最大序列
        //保证相邻字符不相同
        StringBuffer sb = new StringBuffer();
        StringBuffer temp = new StringBuffer();
        for (char c : characters) {
            if (sb.length()>0) {
                if (c != sb.charAt(sb.length()-1)) {
                    sb.append(c);
                }else{
                    temp.append(c);
                }
            }else{
                sb.append(c);
            }
        }
        if (temp.length()>0) {
            String tempStr = handle(temp.toString());//递归调用
            if (sb.charAt(sb.length()-1) != tempStr.charAt(0)) {
                sb.append(tempStr);
            }       
        }
        return sb.toString();
    }
    
    //获取字典排序的最大序列
    public  char[] permutationWithDictionary(char[] characters){
        while(true){
            for (int i = characters.length-1; i >=0; i--) {
                for (int j = 0; j < i; j++) {
                    if (characters[i]>characters[j]) {
                        char temp = characters[i];
                        characters[i] = characters[j];
                        characters[j] = temp;
                        break;
                    }
                }
            }
            //从右向左找到第一个非递增的元素,若找不到则返回结果
            for(int k=characters.length-2;k>=0;k--){    
                if(characters[k]<characters[k+1]){
                    break;                   
                }else if(k==0){
                    return characters;
                }
            }  

        }
    }
    
}

验证

  • 输入:abaa
    输出:aba
  • 输入:abavsbcddab
    输出:vsdcbadbaba

思路

这道题我的思路是,先把字符串分解成char[]数组,然后对数组进行排序获取最大字典序列。
对排序后的数组进行遍历,过滤掉相邻字符相同的部分拼到StringBuffer中,被过滤的字符拼到另一个StringBuffer中。
最后递归调用,得到最终结果。

对于获取最大字典序列,只需要贪心地从后往前遍历每次把更大的字符放到数组前面即可。


CSDN问答上看到的这个题目,解了一下。
解完题发现居然还搜索不到相关答案,因此不敢确定我的答案完全正确。
如果在看的你正好有兴趣,欢迎验证并留言讨论。
期待看到更优的代码~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,281评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,874评论 18 399
  • 第5章 引用类型(返回首页) 本章内容 使用对象 创建并操作数组 理解基本的JavaScript类型 使用基本类型...
    大学一百阅读 3,281评论 0 4
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,793评论 0 33
  • 憋尿总会提高工作效率 仿佛只有拼命写作和思考 才能缓解生理反应的痛 又或者只有在专注工作的时候 才有了对抗尿意的勇...
    陆乙乙阅读 150评论 0 0