1

Trying to solve some of the general programming questions. As part of this, I have tried many ways to achieve the following. For example, I have a string like this

s = "abbcddeeffffcccddddggggghhhiaajjjkk"

I want to find out the maximum successive occurrence of each character in the given string. In the above case, the output should look like,

a - 2
b - 2
c - 3
d - 4
e - 2
f - 4
g - 5 etc
4
  • If you've tried then you need to show us your efforts rather than us giving you the answer straight away Commented May 15, 2015 at 12:50
  • @Cyber,I think I have not described it well, the question is to find out highest successive repeated character count :) Commented May 15, 2015 at 12:54
  • @EdChum, all my efforts are unsuccessful on this, I do not find a valid reason to post unsuccessful programs here Commented May 15, 2015 at 12:57
  • 2
    @pydev Successful or not, posting your attempt shows that you made an effort to solve the problem. As 3rd party observers, we cannot tell the difference between someone being stumped for 1 month trying to solve something vs someone that has homework due in an hour and is asking us to complete their assignment. Commented May 15, 2015 at 12:58

4 Answers 4

5
>>> s = "abbcddeeffffcccddddggggghhhiaajjjkk"
>>> for x in sorted(set(s)):
...   i = 1; 
...   while x * i in s:
...     i += 1
...   print x, "-", i - 1
... 
a - 2
b - 2
c - 3
d - 4
e - 2
f - 4
g - 5
h - 3
i - 1
j - 3
k - 2
Sign up to request clarification or add additional context in comments.

Comments

3

You can find out the length of each group of repeated letters using itertools.groupby, then sort by the length of the groups.

>>> s = "abbcddeeffffcccddddggggghhhiaajjjkk"
>>> from itertools import groupby
>>> repeats = sorted([(letter, len(list(group))) for letter, group in groupby(s)], key = lambda i: i[1], reverse = True)
>>> repeats
[('g', 5), ('f', 4), ('d', 4), ('c', 3), ('h', 3), ('j', 3), ('b', 2), ('d', 2), ('e', 2), ('a', 2), ('k', 2), ('a', 1), ('c', 1), ('i', 1)]
>>> repeats[0]
('g', 5)

1 Comment

You made me found the way to solve this. Perfect!!, Thanks :)
1

A bit of an old thread but contributing for fun.

s = 'aaaabbbbbcdddddddddddd1111000000000000000'

string_set = list(set(list(s)))
string_count_dict = {key: s.count(key) for key in string_set}
print(sorted(string_count_dict.items()))

Output:

[('0', 15), ('1', 4), ('a', 4), ('b', 5), ('c', 1), ('d', 12)]

Comments

0

Mr CoryKramer has given an excellent and a very professional solution solution to the problem. But I think this is an interview question. So I am taking only 1 for loop to do the task. Here is the complete solution. The code is self explanatory.

In Python :

a = "GiniiGinnnaaaaProtiiiijayyyyyyyyyyyyyyyi"

count = 0 
maxcount = 0
lastCharacter = ""
longestcharacter = ""

for ch in a:
    if(ch == lastCharacter):
        count += 1 
        if(count > maxcount):
            maxcount = count
            longestcharacter = ch

    else:
        count = 1 
        lastCharacter = ch

print(longestcharacter)
print(maxcount)        

In Java:

class test2{
    public static void main(String[] args) {
         String aa = "GinaaaaaaaaaaaaaaaPPPPPProtttttijayi";
         char[] ca =  aa.toCharArray();
         char lastchar = 0;
         int maxcount = 0 ;
         int count = 0 ;
         char longestContinuousCharacter = 0;
         for( char ch :ca) {
             if(ch == lastchar) {
                 count++ ;
                 if(count > maxcount) {maxcount = count ;longestContinuousCharacter = ch;}
             }//if
             else {
                 lastchar = ch;
                 count =1 ;
             }//else

         }//for
         System.out.println("longestContinuousCharacter => "+ longestContinuousCharacter);
         System.out.println("maxcount => " + maxcount );

    }//main
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.