Remove N consecutive repeated characters in a string

Related searches

I am trying to solve a problem where the user inputs a string say str = "aaabbcc" and an integer n = 2.

So the function is supposed to remove characters that appearing 'n' times from the str and output only "aaa".

I tried couple of approaches and I'm not able to obtain the right output. Are there any Regular expression functions that I could use or any recursive functions or just plain old iterations.

Thanks in advance.

Using itertools.groupby

Ex:

from itertools import groupby    
s = "aaabbcc"
n = 2

result = ""
for k, v in groupby(s):
    value = list(v)
    if not len(value) == n:
        result += "".join(value)
print(result)

Output:

aaa

Remove all consecutive duplicates from the string, Choose a group of 'k' consecutive identical characters and remove them. static String remove_k_characters(String st1, int n, int k). C program to remove consecutive repeated characters from string. This program will read a string and remove repeated consecutive characters from the string and print new updated string. In this program we will not use another string to copy string after removing the characters; we can say this program is for removing consecutive characters from the string without using another string.

You can use itertools.groupby:

>>> s = "aaabbccddddddddddeeeee"
>>> from itertools import groupby
>>> n = 3
>>> groups = (list(values) for _, values in groupby(s))
>>> "".join("".join(v) for v in groups if len(v) < n)
'bbcc'

Reduce the string by removing K consecutive identical characters , Using itertools.groupby. Ex: from itertools import groupby s = "aaabbcc" n = 2 result = "" for k, v in groupby(s): value = list(v) if not len(value)� Given a string S, remove all the consecutive duplicates. Note that this problem is different from Recursively remove all adjacent duplicates. Here we keep one character and remove all subsequent same characters. Examples: Input : aaaaabbbbbb Output : ab Input : geeksforgeeks Output : geksforgeks Input : aabccba Output : abcba

from collections import Counter

counts = Counter(string)
string = "".join(c for c in string if counts[c] != 2)

Edit: Wait, sorry, I missed "consecutive". This will remove characters that occur exactly two times in the whole string (fitting your example, but not the general case).

Consecutive filter is a bit more complex, but doable - just find the consecutive runs first, then filter out the ones which have length two.

runs = [[string[0], 0]]
for c in string:
    if c == runs[-1][0]:
        runs[-1][1] += 1
    else:
        runs.append([c, 1])
string = "".join(c*length for c,length in runs if length != 2)

Edit2: As the other answers correctly point out, the first part of this is done natively by groupby

from itertools import groupby

string = "".join(c*length for c,length in groupby(string) if length != 2)

Remove N consecutive repeated characters in a string, This program will remove consecutive repeated character from the string in C. else { i++; } } printf("String after removing characaters: %s\n",str); return 0; }. Maximum Consecutive Repeating Character In String. If first character of rem_str matches with the first character of original string, remove the first character from rem_str. are all same). Assume the characters are case – sensitive.

In [15]: some_string = 'aaabbcc'

In [16]: n = 2

In [17]: final_string = ''

In [18]: for k, v in Counter(some_string).items():
    ...:     if v != n:
    ...:         final_string += k * v
    ...:

In [19]: final_string
Out[19]: 'aaa'

You'll need: from collections import Counter

C program to remove consecutive repeated characters from String , This program will read a string and remove repeated consecutive characters from the string and print new printf("String after removing characaters: %s\n",str);. Recur for string of length n-1 (string without first character). Let the string obtained after reducing right substring of length n-1 be rem_str. There are three possible cases If first character of rem_str matches with the first character of original string, remove the first character from rem_str.

from collections import defaultdict
def fun(string,n):
    dic = defaultdict(int)
    for i in string:
        dic[i]+=1
    check = []
    for i in dic:
        if dic[i]==n:
            check.append(i)
    for i in check:
        del dic[i]
    return dic
string = "aaabbcc"
n = 2
result = fun(string, n)
sol =''
for i in result:
    sol+=i*result[i]

print(sol)

output

aaa

C program to remove consecutive repeated characters from string, Example Input Input string: … Continue reading C program to remove all repeated characters in a. For example, this code counts the number of commas within a string. This representation of an n-character string takes n + 1 space (1 for the terminator), and is thus an. Write a program to find the sum of the first 1000 prime numbers.

Maximum consecutive repeating character in string Count of strings that can be formed using a, b and c under given constraints Find largest word in dictionary by deleting some characters of given string

Input string: geeksforgeeks 1) Sort the characters eeeefggkkorss 2) Remove duplicates efgkorskkorss 3) Remove extra characters efgkors. Note that, this method doesn’t keep the original order of the input string. For example, if we are to remove duplicates for geeksforgeeks and keep the order of characters same, then output should be geksfor, but above function returns efgkos.

Another way to remove repeated characters from a string is through the use of a Set. If we do not care about the order of characters in our output string we can use a HashSet. Otherwise, we can use a LinkedHashSet to maintain the insertion order. In both cases, we'll loop over the input string and add each character to the Set.

Comments
  • This solution works as well but I found it a bit hard to understand the 1st edit. Thanks for your approach!