Find one extra character in a string | CPP program to find extra character in one string ; store string values in map ; store second string in map with frequency ; store first string in map with frequency ; if the frequency is 1 then this character is which is added extra ; Driver code ; given string ; find Extra Character
#include <bits/stdc++.h> NEW_LINE 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 ) ; }