从给定的字符串中删除偶数计数的连续重复字符

时间:2018-11-19 15:27:58

标签: python algorithm

我正在尝试解决以下问题:我将字符串作为输入,然后删除偶数的重复字符。

输入:azxxzyyyddddyzzz

输出:azzz

您能帮我吗?

我的尝试可以很好地删除重复的字符,但是我仍然坚持如何删除偶数的重复字符

distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip

4 个答案:

答案 0 :(得分:2)

这里是itertools.groupby的尝试。我不确定是否可以通过更好的时间复杂度来完成。

from itertools import groupby

def rm_even(s):
    to_join = []
    for _, g in groupby(s):
        chars = list(g)
        if len(chars) % 2:
            to_join.extend(chars)
    if to_join == s:
        return ''.join(to_join)
    return rm_even(to_join)

演示:

>>> rm_even('azxxzyyyddddyzzz')
>>> 'azzz'
>>> rm_even('xAAAAx')
>>> ''

答案 1 :(得分:1)

Counter计算字母,并删除具有偶数的字母:

from collections import Counter

word = 'azxxzyyyddddyzzz'
count = Counter(word) # Counter({'z': 5, 'y': 4, 'd': 4, 'x': 2, 'a': 1})
for key, value in count.items():
  if value%2 == 0:
    word = word.replace(key, "")

print(word) # 'azzzzz'

答案 2 :(得分:1)

def remove_even_dup(string):    
    spans = []

    for idx, letter in enumerate(string):
        if not len(spans) or spans[-1][0] != letter:
            spans.append((letter, {idx}))
        else:
            spans[-1][1].add(idx)    

    # reverse the spans so we can use them as a stack
    spans = list(reversed(spans))
    visited = []

    while len(spans):
        letter, indexes = spans.pop()
        if len(indexes) % 2 != 0:
            visited.append((letter, indexes))
        else:
            # if we have any previous spans we might need to merge
            if len(visited):
                prev_letter, prev_indexes = visited[-1]
                next_letter, next_indexes = spans[-1]
                # if the previous one and the next one have the same letter, merge them
                if prev_letter == next_letter:
                    # remove the old
                    visited.pop()
                    spans.pop()
                    # add the new to spans to be visited
                    spans.append((letter, prev_indexes | next_indexes))

    to_keep = { idx for _, indexes in visited for idx in indexes }

    return ''.join(letter for idx, letter in enumerate(string) if idx in to_keep)

答案 3 :(得分:0)

我之所以使用Collection,是因为它很容易删除,我们必须将其转换为字符串。

导入java.util。*;

公共类RemoveEvenCount {

public static void main(String[] args) {

    //String a="azxxzyyyddddyzzz";
    String a="xAAAAx";
    ArrayList a2=new ArrayList<>();
    for(int i=0;i<a.length();i++)
    {
        a2.add(a.charAt(i));            
    }           
    a2=removeData(a2);

    System.out.print(a2);           
}

public static ArrayList removeData(ArrayList a2)
{
    if(a2.size()==2)
    {
        if(a2.get(0)==a2.get(1))
                return null;                
        }

    for(int i=0;i<a2.size();i++)
    {
        int count =1;
        for(int j=0;j<a2.size()-1;j++)
        {
            if(a2.get(j)==a2.get(j+1))
            {
                count++;
            }else               

                if(count%2==0)
            {
                for(int k=0;k<count;k++)
                {
                    a2.remove(j-k);
                }
                return removeData(a2);
            }               
        }       

    }
return a2;
}

}