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:
C++
#include<bits/stdc++.h>
using namespace std;
int FirstRepeated(string str)
{
int checker = 0;
for ( int i = 0; i < str.length(); ++i)
{
int val = (str[i]- 'a' );
if ((checker & (1 << val)) > 0)
return i;
checker |= (1 << val);
}
return -1;
}
int main()
{
string s = "abcfdeacf" ;
int i=FirstRepeated(s);
if (i!=-1)
cout << "Char = " << s[i] << " and Index = " <<i;
else
cout << "No repeated Char" ;
return 0;
}
|
Java
public class First_Repeated_char {
static int FirstRepeated(String str)
{
int checker = 0 ;
for ( int i = 0 ; i < str.length(); ++i)
{
int val = (str.charAt(i)- 'a' );
if ((checker & ( 1 << val)) > 0 )
return i;
checker |= ( 1 << val);
}
return - 1 ;
}
public static void main(String args[])
{
String s = "abcfdeacf" ;
int i=FirstRepeated(s);
if (i!=- 1 )
System.out.println( "Char = " + s.charAt(i) + " and Index = " +i);
else
System.out.println( "No repeated Char" );
}
}
|
Python3
def FirstRepeated(string):
checker = 0
pos = 0
for i in string:
val = ord (i) - ord ( 'a' );
if ((checker & ( 1 << val)) > 0 ):
return pos
checker | = ( 1 << val)
pos + = 1
return - 1
string = "abcfdeacf"
i = FirstRepeated(string)
if i ! = - 1 :
print ( "Char = " , string[i], " and Index = " , i)
else :
print ( "No repeated Char" )
|
C#
using System;
public class First_Repeated_char {
static int FirstRepeated( string str)
{
int checker = 0;
for ( int i = 0; i < str.Length; ++i)
{
int val = (str[i]- 'a' );
if ((checker & (1 << val)) > 0)
return i;
checker |= (1 << val);
}
return -1;
}
public static void Main()
{
string s = "abcfdeacf" ;
int i=FirstRepeated(s);
if (i!=-1)
Console.WriteLine( "Char = " + s[i] +
" and Index = " + i);
else
Console.WriteLine( "No repeated Char" );
}
}
|
PHP
<?php
function FirstRepeated( $str )
{
$checker = 0;
for ( $i = 0; $i < strlen ( $str ); ++ $i )
{
$val = (ord( $str [ $i ]) - ord( 'a' ));
if (( $checker & (1 << $val )) > 0)
return $i ;
$checker |= (1 << $val );
}
return -1;
}
$s = "abcfdeacf" ;
$i =FirstRepeated( $s );
if ( $i !=-1)
echo "Char = " . $s [ $i ] .
" and Index = " . $i ;
else
echo "No repeated Char" ;
?>
|
Javascript
<script>
function FirstRepeated(str)
{
let checker = 0;
for (let i = 0; i < str.length; ++i)
{
let val = (str[i]- 'a' );
if ((checker & (1 << val)) > 0)
return i;
checker |= (1 << val);
}
return -1;
}
let s = "abcfdeacf" ;
let i=FirstRepeated(s);
if (i!=-1)
document.write( "Char = " + s[i] + " and Index = " +i);
else
document.write( "No repeated Char" );
</script>
|
Output
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 write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
15 Jul, 2022
Like Article
Save Article