VOOZH about

URL: https://www.geeksforgeeks.org/dsa/concatenated-string-uncommon-characters-two-strings/

⇱ Find resultant string after concatenating uncommon characters of given strings - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find resultant string after concatenating uncommon characters of given strings

Last Updated : 5 Oct, 2023

Given two strings S1 and S2. The task is to concatenate uncommon characters of the Sto S1  and return the resultant string S1 .

Examples:

Input:S1 = "aacdb", S2 = "gafd"
Output: "cbgf"

Input:S1 = "abcs", S2 = "cxzca";
Output: "bsxz"

Method 1:  Using Hashmap

Approach:

Below is the idea to solve the problem.

The idea is to use Hashmap where the key is a character and the value is an integer i.e. number of strings in which the character is present. If a character is present in one string, then the count is 1, else if the character is present in both strings, the count is 2. 

Follow the steps below to implement the idea:

  • Initialize the result as an empty string.
  • Push all characters of S2 string in map with count as 1.
  • Traverse first string and append all those characters to result that are not present in map then make their count 2.
  • Traverse second string and append all those characters to result whose count is 1.

Below is the implementation of above approach:


Output
bsxz

Time Complexity: O(M + N), where M and N represents the size of the given two strings.
Auxiliary Space: O(max(M, N)), where M and N represents the size of the given two strings.

Method 2: Using Set

Use a set instead of a hashmap to store the characters of the second string. This can simplify the code and reduce the space complexity.

Approach:

  1. Initialize the result as an empty string.
  2. Add all characters of S2 string in a set.
  3. Traverse S1 string and append all those characters to result that are not present in the set.
  4. Return the result.

Below is the implementation of the updated approach:


Output
bszx

Time Complaxity: O(M+N)
Auxiliary Space: O(N)

Method 3: Using Count Arrays

Follow the steps to implement the approach:

  1. Create two arrays, count1 and count2, of size 26 (assuming lowercase English alphabets) initialized with zeros. These arrays will be used to count the occurrences of each character in s1 and s2, respectively.
  2. Iterate over each character in s1 and increment the corresponding count in count1.
  3. Iterate over each character in s2 and increment the corresponding count in count2.
  4. Create an empty string, modified, to store the modified string.
  5. Iterate over each character in s1:
    • If the count of the character in count2 is 0, it is an uncommon character. Append it to modified.
  6. Iterate over each character in s2:
    • If the count of the character in count1 is 0, it is an uncommon character. Append it to modified.
  7. If modified is empty, return -1. Otherwise, return modified.

Below is the implementation:


Output
bsxz

Time complexity: O(m + n), where m and n are the lengths of s1 and s2, respectively. This is because we iterate over both strings.

Space complexity: O(1). The count1 and count2 arrays have a fixed size of 26, so the space required is constant.

Comment