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 email@example.com. 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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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 Nth occurrence of a character in the given String
- Find last index of a character in a string
- Given a string, find its first non-repeating character
- Find k-th character of decrypted string | Set - 2
- Find k'th character of decrypted string | Set 1
- Find the last non repeating character in string
- Find one extra character in a string
- Program to find the kth character after decrypting a string