Repeated Character | Practice | GeeksforGeeks

Given a string consisting of lowercase english alphabets. Find the repeated character present first in the string.
Example 1:
Input:
S = "geeksforgeeks"
Output: g
Explanation: g, e, k and s are the repeating
characters. Out of these, g occurs first. 

â€‹Example 2:
Input: 
S = "abcde"
Output: -1
Explanation: No repeating character present.


Your Task:
You don't need to read input or print anything. Your task is to complete the function firstRep() which takes the string S as input and returns the the first repeating character in the string. In case there's no repeating character present, return '#'.

Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(1).

Constraints:
1<=|S|<=105
 
0
krdmicrosoft5 days ago
JAVA SIMPLE SOLUTION
    char firstRep(String S)
    {
        // your code here
        for(int i = 0;i<S.length()-1;i++)
        {
            if(S.substring(i+1,S.length()).contains(""+S.charAt(i))) return S.charAt(i);
        }
        return '#';
    }
0
yuvanvj0001 week ago
JAVA StringBuffer solution in one loop:
just remove the character at ‘0’ index in string Buffer ,before removing store the the char , after removing check for the same character available in stringbuffer using “contains” method in String, if it is true return the stored char else return ‘#’.
 
 
StringBuffer  sb = new StringBuffer(s);   int  n = sb.length();while(n > 0) { char store = sb.charAt(0); sb.deleteCharAt(0); if(sb.toString().contains(String.valueOf(store))){  return store; } n--; }return '#';
0
kingkrishna43712 weeks ago
#PYTHON CODE...
for i in s:           l=s.count(i)           if(l>1):               return ireturn '#'
0
mayank180919994 weeks ago
 char firstRep (string s)
    {
        //code here.
        unordered_map<char,int>m;
        for(int i=0;i<s.length();i++){
            m[s[i]]++;
        }
        for(int i=0;i<s.length();i++){
            if(m[s[i]]>1){
                return s[i];
            }
        }
     return '#'; 
    }
0
pritamaber1 month ago
No solution can beat this crisp solution
 
logic - for each char from left hand side , check from right hand side weather the char exist on its right
 
    char firstRep (string s)
    {
        //code here.
        for(int i=0; i<s.size(); i++){
            int n = s.size()-1;
            while(n > i){
                if(s[n] == s[i]){
                    return s[i];
                }
                n--;
            }
        }
        return '#';
    }
+1
pratoshbm1 month ago
char firstRep(String S)
    {
        HashMap<Character, Integer> map = new LinkedHashMap<>();
        for(Character c: S.toCharArray())
            map.put(c, map.getOrDefault(c, 0)+1);
        
        for(Map.Entry<Character, Integer> e: map.entrySet()){
            if((int)e.getValue()>1)
                return (char)e.getKey();
        }
        return '#';
    }
0
kumarsurajroy36551 month ago
//User function Template for Javaclass Solution{   char firstRep(String S)   {       // your code here       int arr[] = new int[26];              for(int i=0;i<S.length();i++){                      arr[S.charAt(i)-'a']++;                  }              for(int i=0;i<S.length();i++){                      if(arr[S.charAt(i)-'a']>1){               return S.charAt(i);           }                  }       return '#';   }}
0
amanavengeraman2 months ago
       LinkedHashMap<Character, Integer> hm = new       LinkedHashMap<>(S.length());       Boolean flag = false;       for(int i=0; i<S.length(); i++){           char ch = S.charAt(i);                      if(hm.containsKey(ch)){              hm.put(ch, hm.get(ch)+1);              flag = true;           }else{               hm.put(ch, 1);           }       }              if(!flag) return '#';              // for(int i=0; i<S.length(); i++){       //     int val = hm.get(S.charAt(i));       //     if(val > 1){       //         return S.charAt(i);       //     }       // }              for(Map.Entry map:hm.entrySet()){                      if((int)map.getValue() > 1){               return (char)map.getKey();           }       }              return '#';
0
akkeshri140420012 months ago
char firstRep (string s)   {       //code here.       unordered_map<char,int>m;       char ch;       for(int i=0;i<s.length();i++){           m[s[i]]++;       }       for(int i=0;i<s.length();i++){           if(m[s[i]]>=2){               return s[i];           }       }       return '#';                    }
0
ilihaspatel443 months ago
// I think find fuction would be better
   
char firstRep (string s)   {       for(int i=0;i<s.size();i++){                            string temp=s;             temp.erase(i,1);                      size_t found=temp.find(s[i]);             if(found!=string::npos)             return s[i];       }       return '#' ;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.