Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find the index of first Recurring Character in the given string in Python
In string processing, we often need to find the first recurring character ? the first character that appears again in the string. This problem requires us to track which characters we've seen and return the index where we encounter a character for the second time.
So, if the input is like "abcade", then the output will be 3, as 'a' appears first at index 0 and recurs at index 3.
Approach
To solve this, we will follow these steps ?
- Create a set to store characters we've already seen
- Iterate through each character with its index
- If the character is already in our set, return the current index
- Otherwise, add the character to the set
- If no recurring character is found, return -1
Using Set (Efficient Approach)
This approach uses a set to track seen characters, providing O(n) time complexity ?
def find_first_recurring_index(s):
seen = set()
for i in range(len(s)):
if s[i] in seen:
return i
seen.add(s[i])
return -1
# Test with example
result = find_first_recurring_index("abcade")
print(f"Index of first recurring character: {result}")
# Test with no recurring characters
result2 = find_first_recurring_index("abcdef")
print(f"No recurring character: {result2}")
Index of first recurring character: 3 No recurring character: -1
Using Dictionary (Alternative Approach)
This approach uses a dictionary to count occurrences, though less efficient for this specific problem ?
from collections import defaultdict
def find_first_recurring_with_dict(s):
chars = defaultdict(int)
for i in range(len(s)):
if s[i] in chars:
return i
else:
chars[s[i]] += 1
return -1
# Test the function
result = find_first_recurring_with_dict("abcade")
print(f"Result: {result}")
# Test with string "programming"
result2 = find_first_recurring_with_dict("programming")
print(f"Result for 'programming': {result2}")
Result: 3 Result for 'programming': 3
Comparison of Approaches
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Set-based | O(n) | O(k) | This specific problem |
| Dictionary-based | O(n) | O(k) | When you need character counts |
Note: k is the number of unique characters in the string
Edge Cases
def find_first_recurring_index(s):
seen = set()
for i in range(len(s)):
if s[i] in seen:
return i
seen.add(s[i])
return -1
# Test edge cases
test_cases = [
"", # Empty string
"a", # Single character
"aa", # Immediate repetition
"abcdef", # No repetition
"abcabc" # Multiple repetitions
]
for test in test_cases:
result = find_first_recurring_index(test)
print(f"'{test}' ? {result}")
'' ? -1 'a' ? -1 'aa' ? 1 'abcdef' ? -1 'abcabc' ? 3
Conclusion
The set-based approach is most efficient for finding the first recurring character, offering O(n) time complexity. Use a dictionary approach when you need additional character frequency information beyond just finding the first recurrence.
