Good Subsequence

题目:

Given a word w, a good subsequence of w is defined as a word w' such that

  • all letters in w' are different;
  • w' is obtained from w by deleting some letters in w.

Returns the list of all good subsequences, without duplicates, in lexicographic order
(recall that the sorted() function sorts strings in lexicographic order).

The number of good sequences grows exponentially in the number of distinct letters in w,
so the function will be tested only for cases where the latter is not too large.


题目大意:

给定一个字符串,求该字符串的所有子字符串的集合。要求子字符串中的所有字符都不相同,子字符必须从给定的字符串中获得。此外,子字符串要求按照字典序进行排序。


思路:

首先需要将给定的字符串去重,例如给定的字符串为'aaabbb',去重之后为'ab'。

之后创建结果集合results并为其赋初始值' '。之后遍历'ab'。当遍历到第一位字符'a'时依次遍历results中的字符串,如果字符串不包括字符'a',就在该字符串后加上字符'a'。

例如:

给定字符串'ab'
results = ['']

遍历字符'a'
results = ['', 'a']
遍历字符'b'
results = ['', 'a', 'b','ab']

Note:需要将结果存储在集合(set)中。最后需要将之前的集合results和刚刚获得的集合result合并。集合的合并使用如下的代码:

results = results | result

代码:

def good_subsequences(word):
    '''
    Find good sequences of given string.
    :param word:
    :return:
    '''
    if not word:
        return ''
    str = ridRepetition(word)
    results = set()
    results.add(' ')
    for i in range(len(str)):
        if not results.__contains__(str[i]):
            result = set()
            for ele in results:
                list1 = list(ele)
                if not list1.__contains__(str[i]):
                    result.add(ele + str[i])
            results = results | result
    results = {ele.strip(' ') for ele in results}
    return sorted(results)

def ridRepetition(str):
    '''
    Get rid of repetitive characters.
    :param str:
    :return:
    '''
    new_str = str[0]
    ch = str[0]
    for i in range(len(str)):
        if ch != str[i]:
            new_str = new_str + str[i]
            ch = str[i]
    return new_str

你可能感兴趣的:(Good Subsequence)