![]() |
VOOZH | about |
Implement a space efficient algorithm to check First repeated character in a string without using any additional data structure in one traversal. Use additional data structures like count array, hash, etc is not allowed.
Examples :
Input : abcfdeacf Output : char = a, index= 6
The idea is to use an integer variable and uses bits in its binary representation to store whether a character is present or not. Typically an integer has at-least 32 bits and we need to store presence/absence of only 26 characters.
Implementation:
Char = a and Index = 6
Time Complexity: O(n)
Auxiliary Space: O(1)
This article is contributed by Mr. Somesh Awasthi.