0

I have the below python program to find the maximum number of repeated character and the number of times it is repeated.Since I use s[x] == s[x+1] , to compare a character with the next character , the program complains when it reaches the last index of the string for a index+1.

Please suggest me a way to fix it or a better logic to solve this problem.

s = "abcc"
x=0
count =  1
dict = {}
for x in range(0,len(s)-1):
    if s[x] == s[x+1]:
        count+=1
    else:
        dict[s[x]] = count
        count=1
max_value  = max(dict.values())
max_key = [k for k,v in dict.items() if v == max_value ]
  • Does this answer your question? [Python- find the item with maximum occurrences in a list](https://stackoverflow.com/questions/6987285/python-find-the-item-with-maximum-occurrences-in-a-list) – Kent Shikama Dec 10 '19 at 01:27
  • ^ Note you can turn a string into a list of characters by using `list(s)`. – Kent Shikama Dec 10 '19 at 01:27
  • @JorgeZapatero I don't see any problems. The last element the range will generate is `len(s) - 2`, and the highest element accessed is `len(s) - 1`. – Simon Berens Dec 10 '19 at 01:31
  • For string "aabbaa", do you count only successive repeats (so a only repeats twice), or any repeats, so a repeats 4 times? – DarrylG Dec 10 '19 at 01:35
  • @SimonBerens My bad. You are correct, the second argument of a `range` is excluded – JorgeZapatero Dec 10 '19 at 01:35
  • @darryl the expected output for dictionary will be {"a":1,"b":1,"c":2} – vijay ananth Dec 10 '19 at 01:48
  • @simonBerens my bad...actually there are two things ,1. when i set range as len(s) ,the max index given by range will be 3 , but s[x+1] will search for s[4] which is not existant and complaints an out of range error....2.....when i set the range is len(s)-1 , the max index will be 2 , in this case the dictionary output will not include "c" and just returns , {"a":1,"b":1,} – vijay ananth Dec 10 '19 at 01:52
  • For input s = "aabbbaa" does 'a' have 2 or 4 repeats? Meaning are you just trying to find the longest repeating substring, so bbb would be max with 3 successive repeats? – DarrylG Dec 10 '19 at 02:04
  • @Darryl , yes max would be bbb with three repeats – vijay ananth Dec 10 '19 at 18:21
  • @vijayananth--in that case I'll soon update my answer. – DarrylG Dec 10 '19 at 18:30
  • @vijayananth--updated my answer to reflect you want the letter with the most successive repeats. – DarrylG Dec 10 '19 at 18:45

2 Answers2

0

It's simple, instead start at position 0 and looking for the next one you can start touring the string and adding to dict[s[x]].

s = "caaabcca"
dict = {}

for x in range(0, len(s)):
    dict[s[x]] = 0

for x in range(0, len(s)):
    dict[s[x]] += 1

max_value  = max(dict.values())
max_key = [k for k,v in dict.items() if v == max_value]

print(max_value, max_key)

UPDATED

This idea is interesting and almost the same yours. At start I said my best is the first character and the longest continue secuence has longitude 1 because I'm at first position yet. Next when the character in x and x-1 has been diferents if count is bigger that max I update my best. Otherwise if both characters are equals I only add 1 to count. At the end I must ask again because the string can end with a secuence that maximize the best and it won't introduce in the else.

s = "caaabcca"
c = s[0]
max = 1
count = 1

for x in range(1, len(s)):
    if s[x] == s[x - 1]:
        count += 1
    else:
        if count > max:
            max = count
            c = s[x - 1]
        count = 1

if count > max:
    max = count
    c = s[len(s) - 1]

print(c, max)
Raúl C. Rivero
  • 307
  • 1
  • 4
  • 20
  • 1
    For s = 'abcdabcdabcd" this finds repeats when there are none (i.e. no successive values are the same). – DarrylG Dec 10 '19 at 01:56
  • 1
    I think the question is asking for the longest contiguous substring of a single repeated character, not the character with the highest occurrence. – Simon Berens Dec 10 '19 at 01:56
0

The post is trying to find the letter with the longest successive repeats.

from  itertools import groupby

def create_cnt(s):
  " Find the number of repeats by grouping repetitions 
    together and counting them using Python's itertools groupby function
     (see https://docs.python.org/2/library/itertools.html#itertools.groupby)
    groupby('AAAABBBCCD') --> AAAA BBB CC D

   """
  # create list of tuples (k, count) where
  # k = letter, count = len(list(g)) = number of repeats of letter
  return [(k, len(list(g))) for k, g in groupby(s)]

# Usage Example
for s in ["abcc", "aaabcc", "aabbbaa"]:
  cnts = create_cnt(s)
  # max based upon sublists with largest count (2nd element)
  max_letter, max_count = max(cnts, key=lambda x: x[1])

  print(f'String {s}, Occurences {cnts}, Most Common {max_letter} with {max_count} repeats')

Output

String abcc, Occurences [('a', 1), ('b', 1), ('c', 2)], Most Common c with 2 repeats
String aaabcc, Occurences [('a', 3), ('b', 1), ('c', 2)], Most Common a with 3 repeats
String aabbbaa, Occurences [('a', 2), ('b', 3), ('a', 2)], Most Common b with 3 repeats
DarrylG
  • 16,732
  • 2
  • 17
  • 23