【leetcode】1163. Last Substring in Lexicographical Order
题目如下:
Given a string
s, return the last substring ofsin 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 Module_os_操作系统
目录 目录 前言 软件环境 os模块内建属性 osname 获取执行平台的类型 oslinesep 输出当前平台使用的行终止符 ossep 输出操作系统特定的路径分隔符 ospathsep 输出用于分 ...
- windows下 文件资源管理器 的操作
alt + d 可以直接把光标移动到地址栏 shift + f10 可以触发右键, 后面就可以用键盘操作右键中的内容了 ( 如打开vscode alt + 空格 可以弹出窗口的菜单栏 ( 控制最大化 ...
- 六:flask-自定义URL转换器
flask进行url参数匹配的时候,是继承的werkzeug.routing.BaseConverter库进行重写的 导入看源码 里面有所有的URL参数数据类型的判断 也就是说,可以继承过后实现自己的 ...
- Maven使用WEB-INF/lib下面的jar编译和打包
在某些情况下,maven无法下载依赖的jar,或者依赖的m2会非常的大,上G那是随随便便的事.为了方便修改和编译,在打出的war包基础上,或者直接把tomcat的webapps下的项目拿出来,就可以用 ...
- Win8.1+VS2013+WDK8.1+VirtualBox or VMware 驱动开发环境配置
https://blog.csdn.net/charlessimonyi/article/details/50904956 Win8.1+VS2013+WDK8.1+VirtualBox or VMw ...
- es笔记---新建es索引
es对索引的一堆操作都是用restful api去进行的,参数时一堆json,一年前边查边写搞过一次,这回搞迁移,发现es都到6.0版本了,也变化了很多,写个小笔记记录一下. 创建一个es索引很简单, ...
- [每天一课] 今天就讲一讲关于vue-cli 脚手架里 如何调用API
既然vue-resource停更了,就不讲了,但是效果也是差不多了 今天主要讲一下关于axios的方式来调用API,按照vue-cli的模式,这个当然得先引入一个axios这个模块了.那当然得走一遍终 ...
- 如何上传大文件到github上
真的是被百度坑怕了,只适合我自己的 如果你之前安装git成功的话,右键会出现两个新选项,分别为Git Gui Here,Git Bash Here,这里我们选择Git Bash Here,进入如下界面 ...
- docker--docker compose 编排工具
11 docker compose 编排工具 11.1 docker compose 介绍 根据前面所学的知识可知,想要使用Docker部署应用,就要先在应用中编写Dockerfile 文件来构建镜像 ...
- Balanced Binary Tree(平衡二叉树)
来源:https://leetcode.com/problems/balanced-binary-tree Given a binary tree, determine if it is height ...