Find one extra character in a string
Last Updated :
19 Apr, 2023
Given two strings which are of lengths n and n+1. The second string contains all the characters of the first string, but there is one extra character. Your task is 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 a loop.
- Take two input strings s1 and s2 as inputs.
- Find the length of both strings using the length() function.
- For each character c in s1, iterate over all characters d in s2 until you find a mismatch. If a mismatch is found, break out of the inner loop and continue with the next character in s1. If all characters in s2 match the character c, then c is the extra character and we can return it.
- If we reach the end of s1 and have not found an extra character, then the extra character must be the last character in s2.
- Return the extra character.
C++
#include <iostream>
#include <string>
using namespace std;
char findExtraChar(string s1, string s2)
{
int n1 = s1.length();
int n2 = s2.length();
int i, j;
for (i = 0; i < n1; i++) {
for (j = 0; j < n2; j++) {
if (s1[i] == s2[j]) {
break ;
}
}
if (j == n2) {
return s1[i];
}
}
return s2[n2 - 1];
}
int main()
{
string s1 = "abcd" ;
string s2 = "cbdad" ;
cout << findExtraChar(s1, s2) << endl;
return 0;
}
|
C#
using System;
class Program {
static char FindExtraChar( string s1, string s2)
{
int n1 = s1.Length;
int n2 = s2.Length;
int i, j;
for (i = 0; i < n1; i++) {
for (j = 0; j < n2; j++) {
if (s1[i] == s2[j]) {
break ;
}
}
if (j == n2) {
return s1[i];
}
}
return s2[n2 - 1];
}
static void Main( string [] args)
{
string s1 = "abcd" ;
string s2 = "cbdad" ;
Console.WriteLine(FindExtraChar(s1, s2));
}
}
|
Java
import java.util.*;
public class Main {
static char findExtraChar(String s1, String s2)
{
int n1 = s1.length();
int n2 = s2.length();
int i, j;
for (i = 0 ; i < n1; i++) {
for (j = 0 ; j < n2; j++) {
if (s1.charAt(i) == s2.charAt(j)) {
break ;
}
}
if (j == n2) {
return s1.charAt(i);
}
}
return s2.charAt(n2 - 1 );
}
public static void main(String[] args)
{
String s1 = "abcd" ;
String s2 = "cbdad" ;
System.out.println(findExtraChar(s1, s2));
}
}
|
Python3
def FindExtraChar(s1, s2):
n1 = len (s1)
n2 = len (s2)
for i in range (n1):
for j in range (n2):
if s1[i] = = s2[j]:
break
if j = = n2 - 1 :
return s1[i]
return s2[n2 - 1 ]
s1 = "abcd"
s2 = "cbdad"
print (FindExtraChar(s1, s2))
|
Javascript
function FindExtraChar(s1, s2) {
let n1 = s1.length;
let n2 = s2.length;
let i, j;
for (i = 0; i < n1; i++) {
for (j = 0; j < n2; j++) {
if (s1[i] == s2[j]) {
break ;
}
}
if (j == n2) {
return s1[i];
}
}
return s2[n2 - 1];
}
let s1 = "abcd" ;
let s2 = "cbdad" ;
console.log(FindExtraChar(s1, s2));
|
- Time Complexity:- O(n^2)
- Space Complexity:- O(1)
Method 2: Using Hash Map
Create an empty hash table and insert all character of the second string. Now remove all characters of the first string. The remaining character is the extra character.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
char findExtraCharcter(string strA, string strB)
{
unordered_map< char , int > m1;
for ( int i = 0; i < strB.length(); i++)
m1[strB[i]]++;
for ( int i = 0; i < strA.length(); i++)
m1[strA[i]]--;
for ( auto h1 = m1.begin(); h1 != m1.end(); h1++) {
if (h1->second == 1)
return h1->first;
}
}
int main()
{
string strA = "abcd" ;
string strB = "cbdad" ;
cout << findExtraCharcter(strA, strB);
}
|
Java
import java.io.*;
class GFG
{
static char findExtraCharcter( char []strA, char [] strB)
{
int [] m1 = new int [ 256 ];
for ( int i = 0 ; i < strB.length; i++)
m1[strB[i]]++;
for ( int i = 0 ; i < strA.length; i++)
m1[strA[i]]--;
for ( int i= 0 ;i<m1.length;i++)
{
if (m1[i]== 1 )
return ( char ) i;
}
return Character.MIN_VALUE;
}
public static void main(String[] args)
{
String strA = "abcd" ;
String strB = "cbdad" ;
System.out.println(findExtraCharcter(strA.toCharArray(), strB.toCharArray()));
}
}
|
Python3
def findExtraCharacter(strA, strB):
m1 = {}
for i in strB:
if i in m1:
m1[i] + = 1
else :
m1[i] = 1
for i in strA:
m1[i] - = 1
for h1 in m1:
if m1[h1] = = 1 :
return h1
if __name__ = = "__main__" :
strA = 'abcd'
strB = 'cbdad'
print (findExtraCharacter(strA, strB))
|
C#
using System;
class GFG
{
static char findExtraCharcter( char []strA, char [] strB)
{
int [] m1 = new int [256];
for ( int i = 0; i < strB.Length; i++)
m1[strB[i]]++;
for ( int i = 0; i < strA.Length; i++)
m1[strA[i]]--;
for ( int i = 0; i < m1.Length; i++)
{
if (m1[i]== 1)
return ( char ) i;
}
return char .MinValue;
}
public static void Main(String[] args)
{
String strA = "abcd" ;
String strB = "cbdad" ;
Console.WriteLine(findExtraCharcter(strA.ToCharArray(),
strB.ToCharArray()));
}
}
|
Javascript
<script>
function findExtraCharcter(strA,strB)
{
let m1 = new Array(256);
for (let i = 0; i < 256; i++)
m1[i] = 0;
for (let i = 0; i < strB.length; i++)
m1[strB[i].charCodeAt(0)]++;
for (let i = 0; i < strA.length; i++)
m1[strA[i].charCodeAt(0)]--;
for (let i = 0; i < m1.length; i++)
{
if (m1[i] == 1)
return String.fromCharCode(i);
}
return Number.MIN_VALUE;
}
let strA = "abcd" ;
let strB = "cbdad" ;
document.write(findExtraCharcter(strA.split( "" ), strB.split( "" )));
</script>
|
- Time Complexity:- O(n)
- Auxiliary Space:- O(n).
Method 3(Bits):- traverse first and second string from starting with the xor operation at the end you get the character which is extra.
C++
#include <iostream>
using namespace std;
char findExtraCharcter(string strA, string strB)
{
int res = 0, i;
for (i = 0; i < strA.length(); i++) {
res ^= strA[i];
}
for (i = 0; i < strB.length(); i++) {
res ^= strB[i];
}
return (( char )(res));
}
int main()
{
string strA = "abcd" ;
string strB = "cbdad" ;
cout << findExtraCharcter(strA, strB);
return 0;
}
|
Java
import java.io.*;
class GFG {
static char findExtraCharcter(String strA,
String strB)
{
int res = 0 , i;
for (i = 0 ; i < strA.length(); i++)
{
res ^= strA.charAt(i);
}
for (i = 0 ; i < strB.length(); i++)
{
res ^= strB.charAt(i);
}
return (( char )(res));
}
public static void main(String args[])
{
String strA = "abcd" ;
String strB = "cbdad" ;
System.out.println(findExtraCharcter(strA, strB));
}
}
|
Python 3
def findExtraCharcter(strA, strB) :
res = 0
for i in range ( 0 , len (strA)) :
res = res ^ ( ord )(strA[i])
for i in range ( 0 , len (strB)) :
res = res ^ ( ord )(strB[i])
return (( chr )(res));
strA = "abcd"
strB = "cbdad"
print (findExtraCharcter(strA, strB))
|
C#
using System;
class GFG {
static char findExtraCharcter( string strA,
string strB)
{
int res = 0, i;
for (i = 0; i < strA.Length; i++) {
res ^= strA[i];
}
for (i = 0; i < strB.Length; i++) {
res ^= strB[i];
}
return (( char )(res));
}
public static void Main()
{
string strA = "abcd" ;
string strB = "cbdad" ;
Console.WriteLine(
findExtraCharcter(strA, strB));
}
}
|
PHP
<?php
function findExtraCharcter( $strA , $strB )
{
$res = 0;
for ( $i = 0; $i < strlen ( $strA ); $i ++)
{
$res ^= ord( $strA [ $i ]);
}
for ( $i = 0; $i < strlen ( $strB ); $i ++)
{
$res ^= ord( $strB [ $i ]);
}
return $res ;
}
$strA = "abcd" ;
$strB = "cbdad" ;
echo chr (findExtraCharcter( $strA , $strB ));
?>
|
Javascript
<script>
function findExtraCharcter(strA, strB)
{
let res = 0;
for (let i = 0; i < strA.length; i++)
{
res ^= strA.charCodeAt(i);
}
for (let i = 0; i < strB.length; i++)
{
res ^= strB.charCodeAt(i);
}
return res;
}
let strA = "abcd" ;
let strB = "cbdad" ;
document.write(String.fromCharCode(findExtraCharcter(strA, strB)));
</script>
|
- Time Complexity:- O(n+n+1)
- Space Complexity:- O(1).
Method 4(Character Code):
Add the character codes of both strings. Minus character codes of smaller strings from larger string and convert the resulting integer into a character.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
char findExtraCharacter(string s1, string s2)
{
string smallStr;
string largeStr;
if (s1.size() > s2.size())
{
smallStr = s2;
largeStr = s1;
}
else
{
smallStr = s1;
largeStr = s2;
}
int smallStrCodeTotal = 0;
int largeStrCodeTotal = 0;
int i = 0;
for (; i < smallStr.size(); i++)
{
smallStrCodeTotal += smallStr[i];
largeStrCodeTotal += largeStr[i];
}
largeStrCodeTotal += largeStr[i];
int intChar = largeStrCodeTotal - smallStrCodeTotal;
return ( char )intChar;
}
int main()
{
string s1 = "abcd" ;
string s2 = "cbdae" ;
char extraChar = findExtraCharacter(s1, s2);
cout<< "Extra character: " <<(extraChar)<<endl;
return 0;
}
|
Java
import java.io.*;
public class Test {
private static char findExtraCharacter(String s1, String s2) {
String smallStr;
String largeStr;
if (s1.length() > s2.length()) {
smallStr = s2;
largeStr = s1;
} else {
smallStr = s1;
largeStr = s2;
}
int smallStrCodeTotal = 0 ;
int largeStrCodeTotal = 0 ;
int i = 0 ;
for (; i < smallStr.length(); i++) {
smallStrCodeTotal += smallStr.charAt(i);
largeStrCodeTotal += largeStr.charAt(i);
}
largeStrCodeTotal += largeStr.charAt(i);
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);
}
}
|
Python3
def findExtraCharacter(s1,s2):
smallStr = ""
largeStr = ""
if ( len (s1) > len (s2)):
smallStr = s2
largeStr = s1
else :
smallStr = s1
largeStr = s2
smallStrCodeTotal = 0
largeStrCodeTotal = 0
i = 0
while (i < len (smallStr)):
smallStrCodeTotal + = ord (smallStr[i])
largeStrCodeTotal + = ord (largeStr[i])
i + = 1
largeStrCodeTotal + = ord (largeStr[i])
intChar = largeStrCodeTotal - smallStrCodeTotal
return chr (intChar)
s1 = "abcd"
s2 = "cbdae"
extraChar = findExtraCharacter(s1, s2)
print ( "Extra Character:" , extraChar)
|
C#
using System;
class GFG
{
private static char findExtraCharacter(String s1,
String s2)
{
String smallStr;
String largeStr;
if (s1.Length > s2.Length)
{
smallStr = s2;
largeStr = s1;
}
else
{
smallStr = s1;
largeStr = s2;
}
int smallStrCodeTotal = 0;
int largeStrCodeTotal = 0;
int i = 0;
for (; i < smallStr.Length; i++)
{
smallStrCodeTotal += smallStr[i];
largeStrCodeTotal += largeStr[i];
}
largeStrCodeTotal += largeStr[i];
int intChar = largeStrCodeTotal -
smallStrCodeTotal;
return ( char )intChar;
}
public static void Main(String[] args)
{
String s1 = "abcd" ;
String s2 = "cbdae" ;
char extraChar = findExtraCharacter(s1, s2);
Console.WriteLine( "Extra character: " +
extraChar);
}
}
|
Javascript
<script>
function findExtraCharacter(s1, s2)
{
let smallStr;
let largeStr;
if (s1.length > s2.length)
{
smallStr = s2;
largeStr = s1;
}
else
{
smallStr = s1;
largeStr = s2;
}
let smallStrCodeTotal = 0;
let largeStrCodeTotal = 0;
let i = 0;
for (; i < smallStr.length; i++)
{
smallStrCodeTotal += smallStr[i].charCodeAt(0);
largeStrCodeTotal += largeStr[i].charCodeAt(0);
}
largeStrCodeTotal += largeStr[i].charCodeAt(0);
let intChar = largeStrCodeTotal - smallStrCodeTotal;
return String.fromCharCode(intChar);
}
let s1 = "abcd" ;
let s2 = "cbdae" ;
let extraChar = findExtraCharacter(s1, s2);
document.write( "Extra character: " + extraChar);
</script>
|
Output:
Extra character: e
- Time Complexity:- O(n)
- Auxiliary Space:- O(1)
Share your thoughts in the comments
Please Login to comment...