Home > OS >  find alphabet next to repeated word in a string
find alphabet next to repeated word in a string

Time:10-14

I am making a program that need this:

I want alphabet next to repeated alphabet in python string like str = "abbbbbcde" I want to access c alphabet in string how do I access this. Note : This string is taken as input from user.so it can't be directly indicate as index 6.

CodePudding user response:

Just check if this helps you.

def find_next_to_repeating(string):
    ans = []
    last = second_last = None
    for i in string:
        if second_last and second_last == last and last != i:
            ans.append(i)

        second_last, last = last, i

    return ans


if __name__ == '__main__':
    find_next_to_repeating("abbbcde")

CodePudding user response:

My answer: Let's take an empty list in which we can add distinct elements in the same order from the string. Then we'll count each element. The moment count is >1, we'll print the next value. My code:

s=input("enter")
l=[]
for i in s:
    if i not in l:
        l.append(i) #adding distinct elements
for j in range(len(l)):
    x=s.count(l[j])
    if x>1 and j!=(len(l)-1):
        print(l[j 1]) #if count >0 then print next element
  • Related