题目如下:

Return the lexicographically smallest subsequence of text that contains all the distinct characters of text exactly once.

Example 1:

Input: "cdadabcc"
Output: "adbc"

Example 2:

Input: "abcd"
Output: "abcd"

Example 3:

Input: "ecbacba"
Output: "eacb"

Example 4:

Input: "leetcode"
Output: "letcod"

Note:

  1. 1 <= text.length <= 1000
  2. text consists of lowercase English letters.

解题思路:首先找出每个字母在text中的最大下标值并存入inx列表,接下来把text中的出现的每个字母的存入list并排好序:如果list[0]的字母在text中小标的最小值小于或者inx中所有元素的最小值,则表示该字母可以放在结果的第一个位置;如果不行则继续检查list[1]的字母,直到找出符合条件的字母放在第一位,然后在inx和list中删掉这个字母所对应的记录。之后继续循环查找,直到所有字母都找到为止。

代码如下:

class Solution(object):
def smallestSubsequence(self, text):
"""
:type text: str
:rtype: str
"""
dic = {}
for i in range(len(text)-1,-1,-1):
if text[i] not in dic:
dic[text[i]] = i tl = sorted(dic.iterkeys())
res = ''
start = 0
while len(tl) > 0:
for i in range(len(tl)):
inx = text.find(tl[i],start)
if inx <= min(dic.itervalues()):
res += tl[i]
start = inx + 1
del dic[tl[i]]
del tl[i]
break
return res

【leetcode】1081. Smallest Subsequence of Distinct Characters的更多相关文章

  1. LeetCode 1081. Smallest Subsequence of Distinct Characters

    原题链接在这里:https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/ 题目: Return the le ...

  2. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  3. 【LeetCode】392. Is Subsequence 解题报告(Python)

    [LeetCode]392. Is Subsequence 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/is-subseq ...

  4. [Swift]LeetCode1081. 不同字符的最小子序列 | Smallest Subsequence of Distinct Characters

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  5. 【LeetCode】3. Longest Substring Without Repeating Characters (2 solutions)

    Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...

  6. 【leetcode】1125. Smallest Sufficient Team

    题目如下: In a project, you have a list of required skills req_skills, and a list of people.  The i-th p ...

  7. 【LeetCode】1022. Smallest Integer Divisible by K 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【LeetCode】988. Smallest String Starting From Leaf 解题报告(C++ & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  9. 【LeetCode】910. Smallest Range II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. centos双网卡配置

    centos双网卡问题,一个网卡配置局域网,一个网卡配置公网,如果内网访问自动走eth1,如果外网访问自动走eth2. 需要配置路由吗? 1. 首先查看机器是否是双网卡,命令如下: lspci | g ...

  2. ORACLE Physical Standby DG 之switch over

    DG架构图如下: 计划,切换之后的架构图: DG切换: 主备切换:这里所有的数据库数据文件.日志文件的路径是一致的 [旧主库]主库primarydb切换为备库standby3主库检查switchove ...

  3. ssh连接MAC服务器显示No route to host解决方法

    首先MAC操作系统是10 其它电脑通过ssh访问一台mac服务器时,有时候可以登录进去,有显示显示no route to host 并且通过浏览器访问布署在mac上的jenkins,反应奇慢,后来做了 ...

  4. 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第5节 String类_10_练习:统计输入的字符串中

    char类型在发生数学运算的时候,可以提升为int类型 这就表示char在A到Z之间的

  5. PHP多图片上传 并检查 加水印 源码

    参数说明:$max_file_size : 上传文件大小限制, 单位BYTE$destination_folder : 上传文件路径$watermark : 是否附加水印(1为加水印,其他为不加水印) ...

  6. layui基本使用(动态获取数据,并把需要的数据传到新打开的窗口)

    <div class="xiaoxi">\n' + ' <div class="layui-row">\n' + ' <input ...

  7. 内网渗透 - 提权 - Windows

    MS提权 MS16- MS16- 提权框架 Sherlock 信息收集 ifconfig -a cat /etc/hosts arp -a route -n cat /proc/net/* ping扫 ...

  8. mysql字符串拆分实现split功能

    转自:https://blog.csdn.net/pjymyself/article/details/81668157有分隔符的字符串拆分题目要求数据库中 num字段值为: 实现的效果:需要将一行数据 ...

  9. Java中获取大小:length、length()、size()

    1. java 中的 length 属性是针对数组说的,比如说你声明了一个数组,想知道这个数组的长度则用到了 length 这个属性. 2. java 中的 length() 方法是针对字符串说的,如 ...

  10. 第一个chrome extension

    如今,chrome浏览器的使用如越来越流行,chrome extension往往能提供更多很丰富的功能.以前一直想了解这方面的东西,可是又担心很复杂.前段时间,在斗鱼看一个直播,想刷弹幕,但是每次自己 ...