题目如下:

Given a string s, return the last substring of s in lexicographical order.

Example 1:

  1. Input: "abab"
  2. Output: "bab"
  3. Explanation: The substrings are ["a", "ab", "aba", "abab", "b", "ba", "bab"]. The lexicographically maximum substring is "bab".

Example 2:

  1. Input: "leetcode"
  2. Output: "tcode"

Note:

  1. 1 <= s.length <= 10^5
  2. 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的值为大。

代码如下:

  1. class Solution(object):
  2. def lastSubstring(self, s):
  3. """
  4. :type s: str
  5. :rtype: str
  6. """
  7. max_char = 'a'
  8. for i in s:
  9. max_char = max(max_char,i)
  10.  
  11. item_list = []
  12. sub = ''
  13. for i in s:
  14. if sub == '' and i != max_char:
  15. continue
  16. elif sub == '' and i == max_char:
  17. sub += i
  18. elif sub != '' and i != max_char:
  19. sub += i
  20. elif sub != '' and i == max_char and sub[-1] == max_char:
  21. sub += i
  22. elif sub != '' and i == max_char and sub[-1] != max_char:
  23. item_list.append(sub)
  24. sub = i
  25. elif sub != '' and i != max_char:
  26. sub += i
  27. if len(sub) > 0:item_list.append(sub)
  28.  
  29. print item_list
  30.  
  31. inx = 0
  32. sub = item_list[0]
  33. for i in range(1,len(item_list)):
  34. if item_list[i] == sub:
  35. tmp_inx = i + 1
  36. inx_copy = inx + 1
  37. while inx_copy < len(item_list) and tmp_inx < len(item_list):
  38. if item_list[inx_copy] < item_list[tmp_inx]:
  39. sub = item_list[i]
  40. inx = i
  41. break
  42. inx_copy += 1
  43. tmp_inx += 1
  44. elif len(item_list[i]) < len(sub) and item_list[i] == sub[:len(item_list[i])] and i < len(item_list) - 1:
  45. sub = item_list[i]
  46. inx = i
  47. elif sub < item_list[i] and not (len(sub) < len(item_list[i]) and sub == item_list[i][:len(sub)]):
  48. sub = item_list[i]
  49. inx = i
  50. res = ''
  51. for i in range(inx,len(item_list)):
  52. res += item_list[i]
  53.  
  54. return res

【leetcode】1163. Last Substring in Lexicographical Order的更多相关文章

  1. 【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 ...

  2. 【LeetCode】Longest Palindromic Substring 解题报告

    DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...

  3. 【LeetCode】459. Repeated Substring Pattern 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历子串 日期 [LeetCode] 题目地址:ht ...

  4. 【LeetCode】395. Longest Substring with At Least K Repeating Characters 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...

  5. 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...

  6. 【Leetcode】Longest Palindromic Substring

    问题:https://leetcode.com/problems/longest-palindromic-substring/ 给定一个字符串 S,求出 S 的最长回文子串 思路: 1. 回文:一个字 ...

  7. 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串

    题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...

  8. 【leetcode】Longest Palindromic Substring (middle) 经典

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  9. 【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 ...

随机推荐

  1. 用 Python 解答两道来自阿里伯乐系统的笔试题

    目录 目录 前言 题目一 分析 实现 题目二 分析 实现 前言 朋友到阿里面试,分享两道小题,博主比较闲就试着用 Python 解答一下,实现方式肯定是多种多样的,优劣也会各有不同,欢迎交流. 题目一 ...

  2. fiddler之简单的接口性能测试(replay)

    在针对某一个/某一些接口,发送相同的请求,不考虑参数的变化时,可以使用fiddler进行简单的性能测试.(使用功能为:replay) 一.replay功能调用 (1.Reissue Requests: ...

  3. django项目部署过程

    django项目部署过程 1.上传代码 用git或者其他工具,如scp 代码上传后保证每个应用下的migrations文件夹里只有一个__init__.py文件,自己的迁移文件不要上传上来,具体的gi ...

  4. 操作系统汇编语言之AT&T指令

    转载时格式有问题,大家看原版吧! 作者:EwenWanW  来源:CSDN  原文:https://blog.csdn.net/xiaoxiaowenqiang/article/details/805 ...

  5. AutoML文献阅读

    逐步会更新阅读过的AutoML文献(其实是NAS),以及自己的一些思考 Progressive Neural Architecture Search,2018ECCV的文章: 目的是:Speed up ...

  6. redis配置主从出现DENIED Redis is running in protected mode

    修改redis配置文件,将绑定的ip给注释掉 #127.0.0.1 在配置文件中将protected-mode 改为no protected-mode no 另一种方式是在配置文件中设置密码 requ ...

  7. js 中 json.stringfy()将对象、数组转换成字符串

    json.stringfy()将对象.数组转换成字符串 var student = new Object(); student.name = "Lanny"; student.ag ...

  8. 极*Java速成教程 - (8)

    Java高级特性 注解 注解可以在代码之外添加更多的信息,更加完整地描述程序,帮助编译器进行工作,或者实现某些特定的Java代码之外的功能. 注解可以简化某些重复的流程,自动化那些过程. 注解的使用 ...

  9. 【Python】循环结构中的else

    else在循环结构中,只有循环正常结束后才执行else,如果使用break跳出了循环,不会执行else for i in range(0,10): print(i)else: print(" ...

  10. Python中对 文件 的各种骚操作

    Python中对 文件 的各种骚操作 python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当前Python脚本工作的目录路径: os.getc ...