VOOZH about

URL: https://www.geeksforgeeks.org/dsa/efficiently-find-first-repeated-character-string-without-using-additional-data-structure-one-traversal/

⇱ Efficiently find first repeated character in a string without using any additional data structure in one traversal - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Efficiently find first repeated character in a string without using any additional data structure in one traversal

Last Updated : 15 Jul, 2022

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:


Output
Char = a and Index = 6

Time Complexity: O(n) 
Auxiliary Space: O(1)

This article is contributed by Mr. Somesh Awasthi.  

Comment
Article Tags: