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.
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.
Char = a and Index = 6
Time Complexity: O(n)
Auxiliary Space: O(1)
This article is contributed by Mr. Somesh Awasthi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Efficiently check if a string has all unique characters without using any additional data structure
- Find the first repeated character in a string
- Find repeated character present first in a string
- Count occurrences of a character in a repeated string
- First non-repeating character using one traversal of string | Set 2
- Find a string such that every character is lexicographically greater than its immediate next character
- Find the first repeated word in a string
- Ropes Data Structure (Fast String Concatenation)
- Find the character in first string that is present at minimum index in second string
- Queries to find the first non-repeating character in the sub-string of a string
- Queries to find the last non-repeating character in the sub-string of a given string
- Repeated Character Whose First Appearance is Leftmost
- Find the last non repeating character in string
- Find one extra character in a string
- Find k-th character of decrypted string | Set - 2