Efficiently find first repeated character in a string without using any additional data structure in one traversal Last Updated : 15 Jul, 2022 Comments Improve Suggest changes Like Article Like Report 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= 6Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. 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++ // Efficiently check First repeated character // in C++ program #include<bits/stdc++.h> using namespace std; // Returns -1 if all characters of str are // unique. // Assumptions : (1) str contains only characters // from 'a' to 'z' // (2) integers are stored using 32 // bits int FirstRepeated(string str) { // An integer to store presence/absence // of 26 characters using its 32 bits. int checker = 0; for (int i = 0; i < str.length(); ++i) { int val = (str[i]-'a'); // If bit corresponding to current // character is already set if ((checker & (1 << val)) > 0) return i; // set bit in checker checker |= (1 << val); } return -1; } // Driver code 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 // Efficiently check First repeated character // in Java program public class First_Repeated_char { static int FirstRepeated(String str) { // An integer to store presence/absence // of 26 characters using its 32 bits. int checker = 0; for (int i = 0; i < str.length(); ++i) { int val = (str.charAt(i)-'a'); // If bit corresponding to current // character is already set if ((checker & (1 << val)) > 0) return i; // set bit in checker checker |= (1 << val); } return -1; } // Driver code 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"); } } // This code is contributed by Sumit Ghosh Python3 # Efficiently check First repeated character # in Python # Returns -1 if all characters of str are # unique. # Assumptions : (1) str contains only characters # from 'a' to 'z' ## (2) integers are stored using 32 ## bits def FirstRepeated(string): # An integer to store presence/absence # of 26 characters using its 32 bits. checker = 0 pos = 0 for i in string: val = ord(i) - ord('a'); # If bit corresponding to current # character is already set if ((checker & (1 << val)) > 0): return pos # set bit in checker checker |= (1 << val) pos += 1 return -1 # Driver code string = "abcfdeacf" i = FirstRepeated(string) if i != -1: print ("Char = ", string[i], " and Index = ", i) else: print ("No repeated Char") # This code is contributed by Sachin Bisht C# // C# program to Efficiently // check First repeated character using System; public class First_Repeated_char { static int FirstRepeated(string str) { // An integer to store presence/absence // of 26 characters using its 32 bits. int checker = 0; for (int i = 0; i < str.Length; ++i) { int val = (str[i]-'a'); // If bit corresponding to current // character is already set if ((checker & (1 << val)) > 0) return i; // set bit in checker checker |= (1 << val); } return -1; } // Driver code 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"); } } // This code is contributed by vt_m. PHP <?php // Efficiently check First repeated character // in PHP program // Returns -1 if all characters of str are // unique. // Assumptions : (1) str contains only characters // from 'a' to 'z' // (2) integers are stored using 32 // bits function FirstRepeated($str) { // An integer to store presence/absence // of 26 characters using its 32 bits. $checker = 0; for ($i = 0; $i < strlen($str); ++$i) { $val = (ord($str[$i]) - ord('a')); // If bit corresponding to current // character is already set if (($checker & (1 << $val)) > 0) return $i; // set bit in checker $checker |= (1 << $val); } return -1; } // Driver code $s = "abcfdeacf"; $i=FirstRepeated($s); if ($i!=-1) echo "Char = " . $s[$i] . " and Index = " . $i; else echo "No repeated Char"; // This code is contributed by ita_c ?> JavaScript <script> // Efficiently check First repeated character // in Javascript program function FirstRepeated(str) { // An integer to store presence/absence // of 26 characters using its 32 bits. let checker = 0; for (let i = 0; i < str.length; ++i) { let val = (str[i]-'a'); // If bit corresponding to current // character is already set if ((checker & (1 << val)) > 0) return i; // set bit in checker checker |= (1 << val); } return -1; } // Driver code let s = "abcfdeacf"; let i=FirstRepeated(s); if (i!=-1) document.write("Char = "+ s[i] + " and Index = "+i); else document.write( "No repeated Char"); // This code is contributed by rag2127 </script> OutputChar = a and Index = 6 Time Complexity: O(n) Auxiliary Space: O(1) This article is contributed by Mr. Somesh Awasthi. Comment More infoAdvertise with us Next Article Efficiently find first repeated character in a string without using any additional data structure in one traversal M Mr. Somesh Awasthi Improve Article Tags : Misc Strings Bit Magic Hash DSA +1 More Practice Tags : Bit MagicHashMiscStrings Similar Reads Efficiently check if a string has all unique characters without using any additional data structure Implement an space efficient algorithm to determine if a string (of characters from 'a' to 'z') has all unique characters or not. Use additional data structures like count array, hash, etc is not allowed.Expected Time Complexity : O(n) Examples : Input : str = "aaabbccdaa"Output : NoInput : str = "a 9 min read First non-repeating character using one traversal of string | Set 2 Given a string, find the first non-repeating character in it. For example, if the input string is "GeeksforGeeks", then output should be 'f' and if input string is "GeeksQuiz", then output should be 'G'. Recommended: Please solve it on âPRACTICE â first, before moving on to the solution. We have dis 15+ min read Design a structure which supports insertion and first non-repeating element in O(1) time Design a Data structure which supports insertion and first non-repeating element in O(1) time. Operations that are supported by the data structure: Insertion: Insert a element into the data structure.First non-repeating Element: First non-repeating element into the array. Note: If there is no non-re 12 min read Queries to find the last non-repeating character in the sub-string of a given string Given a string str, the task is to answer Q queries where every query consists of two integers L and R and we have to find the last non-repeating character in the sub-string str[L...R]. If there is no non-repeating character then print -1.Examples: Input: str = "GeeksForGeeks", q[] = {{2, 9}, {2, 3} 11 min read Rearrange characters in a String such that no two adjacent characters are same Given a string s with lowercase repeated characters, the task is to rearrange characters in a string so that no two adjacent characters are the same. If it is not possible to do so, then print empty string ("").Note: Multiple valid rearranged strings can be possible for same input string. Examples: 14 min read Queries to find first occurrence of a character in a given range Given a string S of length N and an array Q[][] of dimension M Ã 3 consisting of queries of type {L, R, C}, the task is to print the first index of the character C in the range [L, R], if found. Otherwise, print -1. Examples: Input: S= "abcabcabc", Q[][] = { { 0, 3, 'a' }, { 0, 2, 'b' }, { 2, 4, 'z' 9 min read Smallest window in a String containing all characters of other String Given two strings s (length m) and p (length n), the task is to find the smallest substring in s that contains all characters of p, including duplicates. If no such substring exists, return "-1". If multiple substrings of the same length are found, return the one with the smallest starting index.Exa 15+ min read Print all the permutations of a string without repetition using Collections in Java Given a string str, the task is to print all the permutations of str. A permutation is an arrangement of all or part of a set of objects, with regard to the order of the arrangement. A permutation should not have repeated strings in the output. Examples: Input: str = "aa" Output: aa Note that "aa" w 2 min read Find repeated character present first in a string Given a string, find the repeated character present first in the string.(Not the first repeated character, found here.) Examples: Input : geeksforgeeks Output : g (mind that it will be g, not e.) Asked in: Goldman Sachs internship Simple Solution using O(N^2) complexity: The solution is to loop thro 15 min read Queue based approach for first non-repeating character in a stream Given a stream of characters and we have to find first non repeating character each time a character is inserted to the stream. Examples: Input : a a b c Output : a -1 b b Input : a a c Output : a -1 cRecommended PracticeFirst non-repeating character in a streamTry It! We have already discussed a Do 5 min read Like