Suppose we have a string s, we have to find the index of the first recurring character in it. If we cannot find no recurring characters, then return -1.
So, if the input is like "abcade", then the output will be 3, as 'a' is again present at index 3.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
from collections import defaultdict class Solution: def solve(self, s): chars = defaultdict(int) for i in range(len(s)): if s[i] in chars: return i else: chars[s[i]] += 1 return -1 ob = Solution() print(ob.solve("abcade"))