【leetcode】1163. Last Substring in Lexicographical Order
题目如下:
Given a string
s
, return the last substring ofs
in lexicographical order.Example 1:
- Input: "abab"
- Output: "bab"
- Explanation: The substrings are ["a", "ab", "aba", "abab", "b", "ba", "bab"]. The lexicographically maximum substring is "bab".
Example 2:
- Input: "leetcode"
- Output: "tcode"
Note:
1 <= s.length <= 10^5
- s contains only lowercase English letters.
解题思路:我的方法是找出s中的最大字符max_char,然后以max_char为分隔符分割s。例如s="azazazzzazbzc",分割后得到item_list : ['za', 'za', 'zzza', 'zb', 'zc'] ,注意这里舍弃了从字符串头部到第一个max_char之间的部分,同时如果有连续多个max_char出现,合并为同一个子串。接下来遍历item_list,找出最大的子串即可,这里有两点要注意,如果item_list中两个元素相等,那么继续比较这两个元素后面的元素,直到找出不一致为止;另外如果一个item是另一个item的前缀字符串,那么较短的item的值为大。
代码如下:
- class Solution(object):
- def lastSubstring(self, s):
- """
- :type s: str
- :rtype: str
- """
- max_char = 'a'
- for i in s:
- max_char = max(max_char,i)
- item_list = []
- sub = ''
- for i in s:
- if sub == '' and i != max_char:
- continue
- elif sub == '' and i == max_char:
- sub += i
- elif sub != '' and i != max_char:
- sub += i
- elif sub != '' and i == max_char and sub[-1] == max_char:
- sub += i
- elif sub != '' and i == max_char and sub[-1] != max_char:
- item_list.append(sub)
- sub = i
- elif sub != '' and i != max_char:
- sub += i
- if len(sub) > 0:item_list.append(sub)
- print item_list
- inx = 0
- sub = item_list[0]
- for i in range(1,len(item_list)):
- if item_list[i] == sub:
- tmp_inx = i + 1
- inx_copy = inx + 1
- while inx_copy < len(item_list) and tmp_inx < len(item_list):
- if item_list[inx_copy] < item_list[tmp_inx]:
- sub = item_list[i]
- inx = i
- break
- inx_copy += 1
- tmp_inx += 1
- elif len(item_list[i]) < len(sub) and item_list[i] == sub[:len(item_list[i])] and i < len(item_list) - 1:
- sub = item_list[i]
- inx = i
- elif sub < item_list[i] and not (len(sub) < len(item_list[i]) and sub == item_list[i][:len(sub)]):
- sub = item_list[i]
- inx = i
- res = ''
- for i in range(inx,len(item_list)):
- res += item_list[i]
- return res
【leetcode】1163. Last Substring in Lexicographical Order的更多相关文章
- 【LeetCode】159. Longest Substring with At Most Two Distinct Characters
Difficulty: Hard More:[目录]LeetCode Java实现 Description Given a string S, find the length of the long ...
- 【LeetCode】Longest Palindromic Substring 解题报告
DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...
- 【LeetCode】459. Repeated Substring Pattern 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历子串 日期 [LeetCode] 题目地址:ht ...
- 【LeetCode】395. Longest Substring with At Least K Repeating Characters 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...
- 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...
- 【Leetcode】Longest Palindromic Substring
问题:https://leetcode.com/problems/longest-palindromic-substring/ 给定一个字符串 S,求出 S 的最长回文子串 思路: 1. 回文:一个字 ...
- 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- 【leetcode】Longest Palindromic Substring (middle) 经典
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 【LeetCode】459. Repeated Substring Pattern
Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...
随机推荐
- 用 Python 解答两道来自阿里伯乐系统的笔试题
目录 目录 前言 题目一 分析 实现 题目二 分析 实现 前言 朋友到阿里面试,分享两道小题,博主比较闲就试着用 Python 解答一下,实现方式肯定是多种多样的,优劣也会各有不同,欢迎交流. 题目一 ...
- fiddler之简单的接口性能测试(replay)
在针对某一个/某一些接口,发送相同的请求,不考虑参数的变化时,可以使用fiddler进行简单的性能测试.(使用功能为:replay) 一.replay功能调用 (1.Reissue Requests: ...
- django项目部署过程
django项目部署过程 1.上传代码 用git或者其他工具,如scp 代码上传后保证每个应用下的migrations文件夹里只有一个__init__.py文件,自己的迁移文件不要上传上来,具体的gi ...
- 操作系统汇编语言之AT&T指令
转载时格式有问题,大家看原版吧! 作者:EwenWanW 来源:CSDN 原文:https://blog.csdn.net/xiaoxiaowenqiang/article/details/805 ...
- AutoML文献阅读
逐步会更新阅读过的AutoML文献(其实是NAS),以及自己的一些思考 Progressive Neural Architecture Search,2018ECCV的文章: 目的是:Speed up ...
- redis配置主从出现DENIED Redis is running in protected mode
修改redis配置文件,将绑定的ip给注释掉 #127.0.0.1 在配置文件中将protected-mode 改为no protected-mode no 另一种方式是在配置文件中设置密码 requ ...
- js 中 json.stringfy()将对象、数组转换成字符串
json.stringfy()将对象.数组转换成字符串 var student = new Object(); student.name = "Lanny"; student.ag ...
- 极*Java速成教程 - (8)
Java高级特性 注解 注解可以在代码之外添加更多的信息,更加完整地描述程序,帮助编译器进行工作,或者实现某些特定的Java代码之外的功能. 注解可以简化某些重复的流程,自动化那些过程. 注解的使用 ...
- 【Python】循环结构中的else
else在循环结构中,只有循环正常结束后才执行else,如果使用break跳出了循环,不会执行else for i in range(0,10): print(i)else: print(" ...
- Python中对 文件 的各种骚操作
Python中对 文件 的各种骚操作 python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当前Python脚本工作的目录路径: os.getc ...