count the same character which comes in sequence

count substrings with same first and last characters
java program to count number of repeated characters in a string
count repeated characters in a string python
how to count the number of occurrences of a character in a string in java
count the number of repeated characters in a given string in c
count occurrences of a character in a repeated string
count number of characters in a string
c program to count the number of occurrences of a character in a string
def count_squences(string):

    i= 0 
    total = 0
    total_char_list = []

    while i < len(string):
        print(string[i])

        if string[i] == "x":
            total += 1
        if string[i] == "y":

            total_char_list.append(total)
            total = 0

        i = i + 1

    return total_char_list


print(count_squences("xxxxyyxyxx"))

I am trying to return the most used x characters in a file format. for example this functions should return [4, 1, 2].

For example if string is "xxxxxyxxyxxx' it should return [5, 2, 3]

My function does not return the correct list. Any helps would be really appreciated. Thanks


You are not resetting your counter when you encounter a y character, and you should only append to total_char_list if there was at least one x character counted by the time you find a y character (y characters could be duplicated too):

total = 0
while i < len(string):
    if string[i] == "x":
        total += 1
    if string[i] == "y":
        if total:
            total_char_list.append(total)
        total = 0

    i = i + 1

Next, when the loop ends and total is not zero, you need to append that value too, or you won't be counting 'x' characters at the end of the sequence:

while ...:
    # ...

if total:
    # x characters at the end
    total_char_list.append(total)

Next, you really want to use a for loop to loop over a sequence. You are given the individual characters that way:

total = 0
for char in string:
    if char == 'x':
        total += 1
    if char == 'y':
        if total:
            total_charlist.append(total)
        total = 0

if total:
    # x characters at the end
    total_char_list.append(total)

You can make this faster with itertools.groupby():

from itertools import groupby

def count_squences(string):
    return [sum(1 for _ in group) for char, group in groupby(string) if char == 'x']

groupby() divides up an iterable input (such as a string) into separate iterators per group, where a group is defined as any consecutive value with the same key(value) result. The default key() function just returns the value, so groupby(string) gives you groups of consecutive characters that are the same. char is the repeated character, and sum(1 for _ in group) takes the length of an iterator.

You can then make it more generic, and count all groups:

def count_all_sequences(string):
    counts = {}
    for char, group in groupby(string):
        counts.setdefault(char, []).append(sum(1 for _ in group))
    return counts

The same can be done with a regular expression:

import re

def count_all_sequences(string):
    counts = {}
    # (.)(\1*) finds repeated characters; (.) matching one, \1 matching the same
    # This gives us (first, rest) tuples, so len(rest) + 1 is the total length
    for char, group in re.findall(r'(.)(\1*)', string):
        counts.setdefault(char, []).append(len(group) + 1)
    return counts

python - count the same character which comes in sequence, def count_squences(string): i= 0 total = 0 total_char_list = [] while i < len(string): print(string[i]) if string[i] == "x": total += 1 if string[i] == "y":  Longest increasing sub-sequence formed by concatenating array to itself N times; Check if given words are present in a string; Count the elements having frequency equals to its value; Count of greater elements for each element in the Array; Check if given string contains all the digits; Count triplets such that sum of any two number is equal to third | Set 2


You don't initialize the value of total between the sequences so it keeps on counting.

def count_squences(string):
    i= 0 
    total = 0
    total_char_list = []
    while i < len(string):
        if string[i] == "x":
            total += 1
        if string[i] == "y":
            if total != 0:
                total_char_list.append(total)
                total = 0
        i = i + 1
    if total != 0:
       total_char_list.append(total)
    return total_char_list

Update (17:00) - fixed the original procedure and I thought of a better solution -

my_str = "xxxxyyxyxx"
[len(z) for z in re.split("y+", my_str)]

How to count a character in an Excel cell or a range?, How do I count certain letters in a column in Excel? Count distinct occurrences as a subsequence. Given a two strings S and T, find count of distinct occurrences of T in S as a subsequence. Examples: Input : S = banana, T = ban Output : 3 T appears in S as below three subsequences. [ban], [ba n], [b an] Input : S = geeksforgeeks, T = ge Output : 6 T appears in S as below three subsequences.


Edited for function format:

def count_sequences(string):
    return [len(x) for x in re.findall(r"x+", string)]

count_sequences("xxxxyyxyxx")

returns [4,1,2]

Determine repeat characters in a php string, How do you find the number of repeating characters in a string? The idea is to process all characters of both strings one by one staring from either from left or right side. Let us traverse from right corner, there are two possibilities for every pair of character being traversed. m: Length of str1 (first string) n: Length of str2 (second string) If last characters of two strings are same, 1.


Count occurrences of a character in a repeated string, How do I count the number of repeated characters in a string in PHP? In this example, frequency of characters in a string object is computed. To do this, size() function is used to find the length of a string object. Then, the for loop is iterated until the end of the string. In each iteration, occurrence of character is checked and if found, the value of count is incremented by 1.


Count substrings with same first and last characters, The string is repeated infinitely. The task is to find the No. of occurrences of a given character x in first N letters. Examples: Input : N = 10 str  Alphabetical order is a system whereby character strings are placed in order based on the position of the characters in the conventional ordering of an alphabet. It is one of the methods of collation. In mathematics, a lexicographical order is the generalization of the alphabetical order to other data types, such as sequences of digits or numbers.


Programming Perl: 3rd Edition, Although the above code works fine, it's not efficient as its time complexity is O(n2​). Note that there are n*(n+1)/2 substrings of a string of length n. This solution also  A simple approach: Store the string to a character array and sort the array. If the characters in the sorted array are in the same order as the string then print ‘In alphabetical order‘. Print ‘Not in alphabetical order‘ otherwise.