Find all Characters in given String which are not present in other String
Given two strings str1 and str2, which are of lengths N and M. The second string contains all the characters of the first string, but there are some extra characters as well. The task is to find all the extra characters present in the second string.
Examples:
Input: str1 = “abcd”, str2 = “dabcdehi”
Output: d, e, h, i
Explanation: [d, e, h, i] are the letters that was added in string str2.
‘d’ is included because in str2 there is one extra ‘d’ than in str1.Input: str1 = “”, str2 = “y”
Output: y
Approach: The solution to this problem is based on the concept of hashing. Follow the steps mentioned below:
- Create an empty hash table of size 26 and increment all occurrences of each character of the first string (str1).
- Now traverse the second string (str2) and remove all characters of first string as well as add the new characters in the hash table.
- Remaining characters in the hash table are the extra characters.
Below is the implementation of the above approach
C++
// C++ program to implement the approach#include <bits/stdc++.h>using namespace std;// Function to find the extra charactersvector<char> findTheDifference(string str1, string str2){ vector<int> vs(26, 0); vector<char> ans; // Loop to find the frequency // of characters in str1 for (int i = 0; i < str1.size(); i++) vs[str1[i] - 'a']++; // Adjusting frequency in str2 for (int i = 0; i < str2.size(); i++) { // If character was present in str1 if (vs[str2[i] - 'a'] > 0) vs[str2[i] - 'a']--; else vs[str2[i] - 'a']++; } // Loop to find the extra characters for (int i = 0; i < 26; i++) if (vs[i] > 0) ans.push_back(i + 'a'); return ans;}// Driver codeint main(){ // Given string string str1 = "abcd"; string str2 = "dabcdehi"; // Function call vector<char> ans = findTheDifference(str1, str2); for (char c : ans) cout << c << " "; return 0;} |
Python3
# python3 program to implement the approach# Function to find the extra charactersdef findTheDifference(str1, str2): vs = [0 for _ in range(26)] ans = [] # Loop to find the frequency # of characters in str1 for i in range(0, len(str1)): vs[ord(str1[i]) - ord('a')] += 1 # Adjusting frequency in str2 for i in range(0, len(str2)): # If character was present in str1 if (vs[ord(str2[i]) - ord('a')] > 0): vs[ord(str2[i]) - ord('a')] -= 1 else: vs[ord(str2[i]) - ord('a')] += 1 # Loop to find the extra characters for i in range(0, 26): if (vs[i] > 0): ans.append(chr(i + ord('a'))) return ans# Driver codeif __name__ == "__main__": # Given string str1 = "abcd" str2 = "dabcdehi" # Function call ans = findTheDifference(str1, str2) for c in ans: print(c, end=" ") # This code is contributed by rakeshsahni |
C#
// C# program for the above approachusing System;using System.Collections.Generic;class GFG{ // Function to find the extra characters static char[] findTheDifference(string str1, string str2) { char[] vs = new char[26]; for (int i = 0; i < 26; i++) { vs[i] = '0'; } char[] ans = new char[1000]; // Loop to find the frequency // of characters in str1 for (int i = 0; i < str1.Length; i++) vs[str1[i] - 'a']++; // Adjusting frequency in str2 for (int i = 0; i < str2.Length; i++) { // If character was present in str1 if (vs[str2[i] - 'a'] > 0) vs[str2[i] - 'a']--; else vs[str2[i] - 'a']++; } // Loop to find the extra characters for (int i = 0; i < 26; i++) if (vs[i] > 0) string.Concat(ans, i + 'a'); return ans; } // Driver Code public static void Main() { // Given string string str1 = "abcd"; string str2 = "dabcdehi"; // Function call char[] ans = findTheDifference(str1, str2); foreach (char c in ans) Console.Write(c + " "); }}// This code is contributed by sanjoy_62. |
Javascript
<script> // JavaScript code for the above approach // Function to find the extra characters function findTheDifference(str1, str2) { let vs = new Array(26).fill(0); let ans = []; // Loop to find the frequency // of characters in str1 for (let i = 0; i < str1.length; i++) vs[str1[i].charCodeAt(0) - 'a'.charCodeAt(0)]++; // Adjusting frequency in str2 for (let i = 0; i < str2.length; i++) { // If character was present in str1 if (vs[str2[i].charCodeAt(0) - 'a'.charCodeAt(0)] > 0) vs[str2[i].charCodeAt(0) - 'a'.charCodeAt(0)]--; else vs[str2[i].charCodeAt(0) - 'a'.charCodeAt(0)]++; } // Loop to find the extra characters for (let i = 0; i < 26; i++) if (vs[i] > 0) ans.push(String.fromCharCode(i + 'a'.charCodeAt(0))); return ans; } // Driver code // Given string let str1 = "abcd"; let str2 = "dabcdehi"; // Function call let ans = findTheDifference(str1, str2); for (let c of ans) document.write(c + " "); // This code is contributed by Potta Lokesh </script> |
d e h i
Time Complexity: O(M)
Auxiliary Space: O(1).


.png)