按字典序排在最后的子串 java实现

Posted tacit-lxs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了按字典序排在最后的子串 java实现相关的知识,希望对你有一定的参考价值。

按字典序排在最后的子串

给你一个字符串 s,找出它的所有子串并按字典序排列,返回排在最后的那个子串。
输入:“abab”
输出:“bab”
解释:我们可以找出 7 个子串 [“a”, “ab”, “aba”, “abab”, “b”, “ba”, “bab”]。按字典序排在最后的子串是 “bab”。
输入:“leetcode”
输出:“tcode”
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/last-substring-in-lexicographical-order
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

  • java实现

代码看着复杂其实很简单,只是求解过程要考虑很多情况而以

class Solution 
    public String lastSubstring(String s) 
       int length = s.length();
        if (length == 1) 
            return s;
        
        char[] charArray = s.toCharArray();
        int startIndex = 0;
        int endIndex = 1;

        while (endIndex < length)  // 滑动窗口末端
            if (charArray[startIndex] < charArray[endIndex])  // 1. 遇到比起始字符大的字符时,窗口整体跳到较大字符处
                startIndex = endIndex;
                endIndex++;
             else if (charArray[startIndex] > charArray[endIndex])  // 2.后续字符小于起始字符时,窗口末端右移
                endIndex++;
             else  // 3.遇到相等的字符时,分别从两个下标开始对比两个子串
                int tempIndex = 1; // 临时增量
                while (endIndex + tempIndex < length)  // 子串滑动窗口末端
                    if (charArray[startIndex] < charArray[endIndex + tempIndex])  // 子串出现较大字符
                        startIndex = endIndex + tempIndex;
                        endIndex = endIndex + tempIndex;
                        break;
                    
                    if (charArray[startIndex + tempIndex] < charArray[endIndex + tempIndex])  // 3.1第二子串较大,窗口起始点跳到第二子串起始处
                        startIndex = endIndex;
                        break;
                    
                    if (charArray[startIndex + tempIndex] > charArray[endIndex + tempIndex])  // 3.2第一子串较大,第一子串窗口末端扩到第二子串末端
                        endIndex = endIndex + tempIndex;
                        break;
                    
                    if (charArray[startIndex + tempIndex] == charArray[endIndex + tempIndex])  // 3.3俩当前子串相等,继续对比俩子串
                        tempIndex++;
                    
                    if (endIndex + tempIndex >= length)  // 3.4第二子串窗口末端到顶部,说明未找到比第一子串大的字符,同3.2,结束对比
                        endIndex = endIndex + tempIndex;
                        break;
                    
                
                endIndex++;
            
        
        return s.substring(startIndex);
    

  • python实现
class Solution:
    def lastSubstring(self, s: str) -> str:
        # 排序最后的字符串必然是从某个位置一直到字符串结束
        # 排序最后的字符串必然是以最大字符开始
        mx = max(s) #取最大的字符
        res = ''
        for i, c in enumerate(s):
            #关键是能比较两个字符串
            if c == mx and s[i:] > res:
                res = s[i:]
        return res

以上是关于按字典序排在最后的子串 java实现的主要内容,如果未能解决你的问题,请参考以下文章

按字典序排在最后的子串 java实现

按字典序排在最后的子串 java实现

按字典序排在最后的子串

按字典序排在最后的子串

POJ-3693/HDU-2459 Maximum repetition substring 最多重复次数的子串(需要输出具体子串,按字典序)

RQNOJ 429 词链:单调栈