0

I went through an interview, where they asked me to print the longest repeated character sequence.

I got stuck is there any way to get it?

But my code prints only the count of characters present in a string is there any approach to get the expected output

import pandas as pd
import collections

a   = 'abcxyzaaaabbbbbbb'
lst = collections.Counter(a)
df  = pd.Series(lst)
df

Expected output :

bbbbbbb

How to add logic to in above code?

9
  • 2
    This does not need pandas. Commented Oct 24, 2021 at 5:57
  • @Psidom : I was giving it a try but don't know how to implement logic checking is it possible Commented Oct 24, 2021 at 5:58
  • 2
    res = max(("".join(g) for _, g in groupby(a)), key=len) -> 'bbbbbbb'. Here, groupby is itertools.groupby Commented Oct 24, 2021 at 6:00
  • @Ch3steR : can you explain in detail no idea what is happening Commented Oct 24, 2021 at 6:01
  • 1
    Few relevant links: Get the largest string using max, How do I use itertools.groupby Commented Oct 24, 2021 at 6:09

3 Answers 3

3

A regex solution:

max(re.split(r'((.)\2*)', a), key=len)

Or without library help (but less efficient):

s = ''
max((s := s * (c in s) + c for c in a), key=len)

Both compute the string 'bbbbbbb'.

Sign up to request clarification or add additional context in comments.

9 Comments

@RolfofSaxony Hmm, what's the point of repeating that? That's just the expected output already shown in the question. I might show my output if it deviated from the expected output (like yours does), but if it's just what's expected...
Feel free to reject the edit, I simply added it for clarity
You could improve the efficiency of the second one a little bit by changing (c in s) to s.endswith(c)
@AlainT. Yes, and I had considered c in s[:1], but neither change the worst case complexity, so I opted for brevity/beauty :-)
@AlainT. Hmm, actually... c in s is amortized O(1) here. And quite possibly faster than loading and calling the endswith method.
|
2

Without any modules, you could use a comprehension to go backward through possible sizes and get the first character multiplication that is present in the string:

next(c*s for s in range(len(a),0,-1) for c in a if c*s in a)

That's quite bad in terms of efficiency though

another approach would be to detect the positions of letter changes and take the longest subrange from those

chg = [i for i,(x,y) in enumerate(zip(a,a[1:]),1) if x!=y]
s,e = max(zip([0]+chg,chg+[len(a)]),key=lambda se:se[1]-se[0])
longest = a[s:e]

Of course a basic for-loop solution will also work:

si,sc = 0,"" # current streak (start, character)
ls,le = 0,0  # longest streak (start, end)
for i,c in enumerate(a+" "):      # extra space to force out last char.
    if i-si > le-ls: ls,le = si,i # new longest
    if sc != c:      si,sc = i,c  # new streak
longest = a[ls:le]

print(longest) # bbbbbbb

Comments

1

A more long winded solution, picked wholesale from:
maximum-consecutive-repeating-character-string

def maxRepeating(str):
 
    len_s = len(str)
    count = 0
 
    # Find the maximum repeating
    # character starting from str[i]
    res = str[0]
    for i in range(len_s):
         
        cur_count = 1
        for j in range(i + 1, len_s):
            if (str[i] != str[j]):
                break
            cur_count += 1
 
        # Update result if required
        if cur_count > count :
            count = cur_count
            res = str[i]
    return res, count
 
# Driver code
if __name__ == "__main__":
    str = "abcxyzaaaabbbbbbb"
    print(maxRepeating(str))

Solution:

('b', 7)

2 Comments

Did you pick the inefficient one intentionally?
@don'ttalkjustcode Ha! Why yes, as a matter of fact I did. Simply because of the environment which caused this question to be asked, namely an interview, without access to SO. I had a sneaking suspicion that this question might attract more than a few solutions. :)

Your Answer

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