【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 ...
随机推荐
- CentOS 6、CentOS7 防火墙开放指定端口
当我们在CentOS服务器中装了一些开发环境(如 tomcat.mysql.nginx 等...)时,希望能从外界访问,就需要配置防火墙对指定端口开放. CentOS 6.51.开放指定端口/sbin ...
- 开发一个Flink应用
步骤列表本次实战经历以下步骤: 创建应用:编码:构建:提交任务到Flink,验证功能: 环境信息Flink:1.7:Flink所在机器的操作系统:CentOS Linux release 7.5.18 ...
- 用JS实现移动的窗口
https://blog.csdn.net/iteye_21064/article/details/81496640 用JS实现移动的窗口 2007年09月06日 23:23:00 阅读数:3 很简单 ...
- 10 oracle bbed恢复ora-600[4193][4194]的错误
ORA ] 错误解析 ERROR: Format: ORA ] [a] [b] VERSIONS: versions 6.0 to 12.1 DESCRIPTION: A mismatch has b ...
- c++ 调用 sqlite
#include <iostream> #include "sqlite3.h" using namespace std; void dbTest() { #pragm ...
- CM使用MySQL数据库预处理scm_prepare_database.sh执行报错:java.sql.SQLException: Access denied for user 'scm'@'hadoop101.com' (using password: YES)
1.报错提示: [root@hadoop101 ~]# /opt/module/cm/cm-/share/cmf/schema/scm_prepare_database.sh mysql cm -hh ...
- windows上zeal安装和使用--离线API文档
1.官网:https://zealdocs.org/download.html#windows 2.github:https://github.com/zealdocs/zeal 3.下载:可下载安装 ...
- POJ练习计划
题目链接:https://cn.vjudge.net/article/348 2019/7/24: [POJ-1423] [题解] [POJ-1503] 模板题
- 学习Golang语言(6):类型--切片
学习Golang语言(1): Hello World 学习Golang语言(2): 变量 学习Golang语言(3):类型--布尔型和数值类型 学习Golang语言(4):类型--字符串 学习Gola ...
- 爬虫之selenium 安装与 chromedriver安装
今天学到一个有意思的插件,就是chromedriver,在爬虫的时候,如果网站反爬虫做的很好,自己又很想爬去里面的数据,那就可以用这个插件,虽然笨笨的,慢的一批,但是还有别的办法就不会用他啦, 这个东 ...