【leetcode】1081. Smallest Subsequence of Distinct Characters

题目如下:

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

原文地址:https://www.cnblogs.com/seyjs/p/11026152.html

时间: 2024-10-09 12:17:29

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

【Leetcode】Kth Smallest Element in a Sorted Matrix

题目链接:https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/ 题目: Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix. Note that it is the kth smallest eleme

【Leetcode】Longest Increasing Subsequence

题目链接:https://leetcode.com/problems/longest-increasing-subsequence/ 题目: Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2,

【Leetcode】Increasing Triplet Subsequence

题目链接:https://leetcode.com/problems/increasing-triplet-subsequence/ 题目: Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. Formally the function should: Return true if there exists i, j, k such tha

【Leetcode】376. Wiggle Subsequence

Description: A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fe

【LeetCode】Increasing Triplet Subsequence(334)

1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. Formally the function should: Return true if there exists i, j, k such that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤

【leetcode】988. Smallest String Starting From Leaf

题目如下: Given the root of a binary tree, each node has a value from 0 to 25representing the letters 'a' to 'z': a value of 0 represents 'a', a value of 1 represents 'b', and so on. Find the lexicographically smallest string that starts at a leaf of thi

【LeetCode】3. Longest Substring Without Repeating Characters

题目: Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of

【LeetCode】003 Longest Substring Without Repeating Characters

题目:LeetCode 003 Longest Substring Without Repeating Characters 题意:给一个字符串,找到一个没有重复字符的最长子串. 样例:”bbbbb” 返回”b”的长度1:”abcabcbb”返回”abc”的长度3. 思路: 动态规划.dp[i]表示以第i个字符为结尾的无重复字符的子串的最长的长度,需要一个辅助数组idx[s[i]]记录字符当前字符s[i]上一次出现的位置,如果未出现过则为-1.所以得到如下的递推公式: 另外由于不确定字符串长度的

【leetcode】Distinct Subsequences

问题:给定两个字符串S,T,对于S,可以删除其中的任意多个(包括0)字符,使其得到T.问有多少种删法可以得到T. 举例分析: S:ababa T: aba dp[i][j] : 表示 S 从0 ~ i - 1,T从0~j - 1,所得到的方法数.i,j 表示长度. 初始条件:dp[i][0] = 1,T为空串,而空串总是任意串的字串.即,将S串的所有字符都删掉,就得到T. 状态转移方程: dp[ i ] [ j ] = dp[ i - 1] [ j - 1] + dp[ i - 1] [ j ]