美文网首页Leetcode数据结构和算法分析
算法题目解题记录——BestString

算法题目解题记录——BestString

作者: 三汪 | 来源:发表于2017-11-22 18:00 被阅读10次

本文由作者三汪首发于简书。
历史解题记录已同步更新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问答上看到的这个题目,解了一下。
解完题发现居然还搜索不到相关答案,因此不敢确定我的答案完全正确。
如果在看的你正好有兴趣,欢迎验证并留言讨论。
期待看到更优的代码~

相关文章

  • 算法题目解题记录——BestString

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

  • leetcode 刷题方法

    按照题目考察的数据结构分类 每道题目记录关键的提示,考察的算法思想 归纳题型以及对应的解题技巧

  • leetcode第77题:组合 [中等]

    题目描述 考点 回溯算法 解题思路 代码实现

  • leetcode第79题:单词搜索

    题目描述 考点 数组 回溯算法 解题思路 visited 用于记录当前位置,是否被访问过;访问过的不能再次使用; ...

  • leetcode第55题:跳跃游戏

    题目描述 考点 数组 贪心算法 解题思路 从头遍历数组,使用reach记录能够到达的最远位置;(1)如果当前遍历的...

  • LeetCode Record C++

    本文内容为练习LeetCode题目时的解题思路和不同算法的记录,实现语言为C++,代码保存在Github,均已在L...

  • 学习记录几道算法题 - 递归练习

    前言 来啦老铁! 今天记录一下最近遇到的几个算法相关的题目,也可能算不上算法,只能算是巧妙的解题方法,一起来瞅一瞅...

  • Leetcode 2050 Parallel Courses I

    今天刷到了这样一道题, 同时涉及了拓扑排序和动态规划算法,记录一下解题思路题目如下 You are given a...

  • LeetCode每日一题 之 矩阵中的路径

    题目 image 题目地址 解题思路 这道题和之前的一道机器人走格子的算法题很像,都是根据深度优先的回溯方法解题。...

  • ORID39 Deque window slide

    今天做了有关window slide相关题目 [239] 解题报告 window slide 用什么算法? win...

网友评论

    本文标题:算法题目解题记录——BestString

    本文链接:https://www.haomeiwen.com/subject/bzdovxtx.html