I have a question about the "First recurring characters" problem (Python) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I have a question about the "First recurring characters" problem (Python)

I can find a lot of codes that work for this. But the thing that I want to ask about is that if the given string is "ABBA", the codes I get return B because B is the first repeated char, but I want a code that could return A because A is the first char that has repetition. The code I found about this looks complicated and I can't understand it, can someone give me a (Python) code for it with comments for explanations? Much appreciated, thanks.

23rd Aug 2019, 11:52 AM
Ahmad Faris Khawarizmi
Ahmad Faris Khawarizmi - avatar
7 Answers
+ 2
you could do that like this: go through the string at each character test if it's in the rest of the string(everything right of where you are in the String right now) if there is one, that will be your result else continue with the next character
23rd Aug 2019, 12:19 PM
Anton Böhler
Anton Böhler - avatar
23rd Aug 2019, 12:25 PM
Anton Böhler
Anton Böhler - avatar
+ 2
Airree question duplication happens when your connection isn't that great, it happens to me so often😩 (if you look at the time of the post, you'll see that their posted at the exact same time)
23rd Aug 2019, 12:28 PM
Anton Böhler
Anton Böhler - avatar
+ 1
Anton Böhler I am aware of that, but the question still can be deleted
23rd Aug 2019, 12:30 PM
Airree
Airree - avatar
+ 1
Thanks everyone
23rd Aug 2019, 1:03 PM
Ahmad Faris Khawarizmi
Ahmad Faris Khawarizmi - avatar
0
Please don't ask the same question twice I know little about python, but I know that there are regular expressions. I made a javascript code which works, and if you can implement it in python, then good for you, you got yourself an answer.
23rd Aug 2019, 12:19 PM
Airree
Airree - avatar
0
Note: the code will return the first character that has repetition in the string. A will be returned when string is 'ABBA' string =list(input()) for i in range(len(string)): a=string[i] string.pop(i) if i==len(string)-1: print('No recurring characters.') break if a in string: print(a +' is the recurring character.') break
23rd Aug 2019, 12:21 PM
Ketchup🍅
Ketchup🍅 - avatar