Find one extra character in a string

Given two strings which are of lengths n and n+1. The second string contains all the character of the first string, but there is one extra character. Your task to find the extra character in the second string.

Examples :

Input : string strA = "abcd";
        string strB = "cbdae";
Output : e
string B contain all the element 
there is a one extra character which is e

Input : string strA = "kxml";
        string strB = "klxml";
Output : l
string B contain all the element 
there is a one extra character which is l



Method 1(Brute Force):-
Check with two for loop.
Time Complexity:- O(n^2)
Space Complexity:- O(1).

Method 2(Hash Map):-
Create an empty hash table and insert all character of second string. Now remove all characters of first string. Remaining character is the extra character.

Time Complexity:- O(n)
Auxiliary Space:- O(n).

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find extra character in one 
// string
#include <bits/stdc++.h>
using namespace std;
  
char findExtraCharcter(string strA, string strB)
{
    // store string values in map
    unordered_map<char, int> m1;
  
    // store second string in map with frequency
    for (int i = 0; i < strB.length(); i++)
        m1[strB[i]]++;
  
    // store first string in map with frequency
    for (int i = 0; i < strA.length(); i++)
        m1[strA[i]]--;
  
    for (auto h1 = m1.begin(); h1 != m1.end(); h1++) {
  
        // if the frequency is 1 then this
        // character is which is added extra
        if (h1->second == 1)
            return h1->first;
    }
}
  
int main()
{
    // given string
    string strA = "abcd";
    string strB = "cbdad";
  
    // find Extra Character
    cout << findExtraCharcter(strA, strB);
}

chevron_right


Output:

d

Method 3(Bits):-
traverse first and second string from starting with xor operation at the end you get the character which is extra.
Time Complexity:- O(n+n+1)
Space Complexity:- O(1).

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find extra character in one 
// string
#include <iostream>
using namespace std;
  
char findExtraCharcter(string strA, string strB)
{
    // result store the result
    int res = 0, i;
  
    // traverse string A till end and 
    // xor with res
    for (i = 0; i < strA.length(); i++) {
  
        // xor with res
        res ^= strA[i];
    }
  
    // traverse string B till end and 
    // xor with res
    for (i = 0; i < strB.length(); i++) {
  
        // xor with res
        res ^= strB[i];
    }
  
    // print result at the end
    return ((char)(res));
}
  
int main()
{
    // given string
    string strA = "abcd";
    string strB = "cbdad";
    cout << findExtraCharcter(strA, strB);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find extra
// character in one string
import java.io.*;
  
class GFG {
      
    static char findExtraCharcter(String strA,
                                  String strB)
    {
        // result store the result
        int res = 0, i;
      
        // traverse string A till 
        // end and xor with res
        for (i = 0; i < strA.length(); i++)
        {
            // xor with res
            res ^= strA.charAt(i);
        }
      
        // traverse string B till end and 
        // xor with res
        for (i = 0; i < strB.length(); i++)
        {
            // xor with res
            res ^= strB.charAt(i);
        }
      
        // print result at the end
        return ((char)(res));
    }
      
    // Driver code
    public static void main(String args[])
    {
        // given string
        String strA = "abcd";
        String strB = "cbdad";
        System.out.println(findExtraCharcter(strA, strB));
    }
}
  
/*This code is contributed by Nikita Tiwari.*/

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to find 
# extra character in one string
  
def findExtraCharcter(strA, strB) :
      
    # result store the result
    res = 0
  
    # traverse string A till 
    # end and xor with res
    for i in range(0,len(strA)) :
          
        # xor with res
        res =res ^ (ord)(strA[i])
          
    # traverse string B till 
    # end and xor with res
    for i in range(0,len(strB)) :
          
        # xor with res
        res = res ^ (ord)(strB[i])
          
    # print result at the end
    return ((chr)(res));
  
# given string
strA = "abcd"
strB = "cbdad"
print(findExtraCharcter(strA, strB))
  
# This code is contributed by Nikita Tiwari.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find extra character
// in one string
using System;
  
class GFG {
  
    static char findExtraCharcter(string strA, 
                                  string strB)
    {
        // result store the result
        int res = 0, i;
      
        // traverse string A till end and 
        // xor with res
        for (i = 0; i < strA.Length; i++) {
      
            // xor with res
            res ^= strA[i];
        }
      
        // traverse string B till end and 
        // xor with res
        for (i = 0; i < strB.Length; i++) {
      
            // xor with res
            res ^= strB[i];
        }
      
        // print result at the end
        return ((char)(res));
    }
      
    // Driver Code
    public static void Main()
    {
        // given string
        string strA = "abcd";
        string strB = "cbdad";
        Console.WriteLine(
            findExtraCharcter(strA, strB));
    }
}
  
// This code is contributed by Manish Shaw 
// (manishshaw1)

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find extra character in
// one string
  
function findExtraCharcter($strA, $strB)
{
    // result store the result
    $res = 0;
  
    // traverse string A till end and 
    // xor with res
    for ($i = 0; $i < strlen($strA); $i++)
    {
  
        // xor with res
        $res ^= ord($strA[$i]);
    }
  
    // traverse string B till end and 
    // xor with res
    for ($i = 0; $i < strlen($strB); $i++)
    {
  
        // xor with res
        $res ^= ord($strB[$i]);
    }
  
    // print result at the end
    return $res;
}
  
// Driver code
$strA = "abcd";
$strB = "cbdad";
echo chr(findExtraCharcter($strA, $strB));
  
// This code is contributed by Manish Shaw
// (manishshaw1)
?>

chevron_right


Output:

d

Method 4(Character Code):-
Add the character codes of both the strings. Minus character codes of smaller string from larger string and covert the result integer into character.

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

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find extra
// character in one string
  
public class Test {
  
    private static char findExtraCharacter(String s1, String s2) {      
        String smallStr;
        String largeStr;
  
        // Determine String with extra character.
        if(s1.length() > s2.length()) {
            smallStr = s2;
            largeStr = s1;
        } else {
            smallStr = s1;
            largeStr = s2;
        }
  
        int smallStrCodeTotal = 0;
        int largeStrCodeTotal = 0;
        int i = 0;
  
        // Add character codes of both the strings
        for(; i < smallStr.length(); i++) {
            smallStrCodeTotal += smallStr.charAt(i);
            largeStrCodeTotal += largeStr.charAt(i);
        }
  
        // Add last character code of large String.
        largeStrCodeTotal += largeStr.charAt(i);
  
        // Minus the character code of smaller string from 
        // the character code of large string. 
        // The result will be the extra character code.
        int intChar = largeStrCodeTotal - smallStrCodeTotal;        
        return (char)intChar;
    }
      
    public static void main(String[] args) {
        String s1 = "abcd";
        String s2 = "cbdae";
          
        char extraChar = findExtraCharacter(s1, s2);
        System.out.println("Extra character: " + extraChar);
          
    }
}
  
  
/*This code is contributed by Amol Bhosale.*/

chevron_right


Output:

Extra character: e


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.