Open In App

Concatenated string with uncommon characters in Python

Last Updated : 12 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Two strings are given and you have to modify the 1st string such that all the common characters of the 2nd string have to be removed and the uncommon characters of the 2nd string have to be concatenated with the uncommon characters of the 1st string. 

Examples:

Input : S1 = “aacdb”, S2 = “gafd”
Output : “cbgf”
Input : S1 = “abcs”;, S2 = “cxzca”;
Output : “bsxz”

This problem has an existing solution please refer Concatenated string with uncommon characters of two strings link. We can solve this problem quickly in Python using Set and List Comprehension. The approach is simple,

  1. Convert both strings into a set so that they could have only unique characters. Now take the intersection of two sets to get the common character both strings have.
  2. Now separate out those characters in each string that are not common in both of them and concatenate the characters.

Implementation:

Python3




# Function to concatenated string with uncommon
# characters of two strings
 
def uncommonConcat(str1, str2):
 
    # convert both strings into set
    set1 = set(str1)
    set2 = set(str2)
 
    # take intersection of two sets to get list of
    # common characters
    common = list(set1 & set2)
 
    # separate out characters in each string
    # which are not common in both strings
    result = [ch for ch in str1 if ch not in common] + [ch for ch in str2 if ch not in common]
 
    # join each character without space to get
    # final string
    print( ''.join(result) )
 
# Driver program
if __name__ == "__main__":
    str1 = 'aacdb'
    str2 = 'gafd'
    uncommonConcat(str1,str2)


Output

cbgf

Time Complexity: O(n+m), where n is length of str1 and m is length of str2
Auxiliary Space: O(m), where m is length of result list.

Approach#2: Using set symmetric difference 

We can use the symmetric difference operation of set to pull out all the uncommon character from both the string and make a string. 

Implementation:

Python3




# Function to concatenated string with uncommon
# characters of two strings
 
def uncommonConcat(str1, str2):
 
    # convert both strings into set
    set1 = set(str1)
    set2 = set(str2)
 
    # Performing symmetric difference operation of set
    # to pull out uncommon characters
    uncommon = list(set1 ^ set2)
 
    # join each character without space to get
    # final string
    print( ''.join(uncommon) )
 
# Driver program
if __name__ == "__main__":
    str1 = 'aacdb'
    str2 = 'gafd'
    uncommonConcat(str1,str2)


Output

fbgc

Approach #3: Using Counter() function

Python3




# Function to concatenated string with uncommon
# characters of two strings
from collections import Counter
 
 
def uncommonConcat(str1, str2):
 
    result = []
    frequency_str1 = Counter(str1)
    frequency_str2 = Counter(str2)
    for key in frequency_str1:
        if key not in frequency_str2:
            result.append(key)
            for key in frequency_str2:
                if key not in frequency_str1:
                    result.append(key)
 
    # Sorting the result
    result.sort()
 
    print(''.join(set(result)))
 
 
# Driver program
if __name__ == "__main__":
    str1 = 'aacdb'
    str2 = 'gafd'
    uncommonConcat(str1, str2)


Output

cfbg

Approach #4 : Using two loops and no built-in module

In this approach we will use two loops and a variable to store the final result. We will iterate over the each string once and check that certain element is present in the other string or not, if not then we will just concatenate that character with our variable.

Python3




# Function to concatenate uncommon characters
# of two different strings
 
def concatenate_uncommon(str1,str2):
     
    # variable to store
    # the final string
     
    final_str = ''
     
    # iterating over first string
    # and checking each character is present
    # in the other string or not
    # if not then simply store that character
    # in the variable
     
    for i in str1:
        if i in str2:
            pass
        else:
            final_str+=i
             
    # iterating over second string
    # and checking each character is present
    # in the other string or not
    # if not then simply store that character
    # in the same variable as earlier
     
         
    for j in str2:
        if j in str1:
            pass
        else:
            final_str+=j
     
    # returning the final string as result
    return final_str
 
 
# Driver Code
 
# Example - 1
str1 = 'abcs'
str2 = 'cxzca'
print(concatenate_uncommon(str1,str2))
 
# Example - 2
str1 = 'aacdb'
str2 = 'gafd'
print(concatenate_uncommon(str1,str2))


Output

bsxz
cbgf
Time Complexity - O(n+m) # n = length of string 1 and m = length of string 2.
Auxiliary Space - O(1) # only one extra variable has been used


Similar Reads

Python - String uncommon characters
One of the string operation can be computing the uncommon characters of two strings i.e, output the uncommon values that appear in both strings. This article deals with computing the same in different ways. Method 1: Using set() + symmetric_difference() Set in python usually can perform the task of performing set operations such as set symmetric di
5 min read
Find resultant string after concatenating uncommon characters of given strings
Given two strings S1 and S2. The task is to concatenate uncommon characters of the S2 to S1 and return the resultant string S1 . Examples: Input: S1 = "aacdb", S2 = "gafd"Output: "cbgf" Input: S1 = "abcs", S2 = "cxzca";Output: "bsxz" Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. Method 1: Using Hashmap Approac
13 min read
Find uncommon characters of the two strings | Set 2
Given two strings, str1 and str2, the task is to find and print the uncommon characters of the two given strings in sorted order without using extra space. Here, an uncommon character means that either the character is present in one string or it is present in the other string but not in both. The strings contain only lowercase characters and can c
6 min read
Find uncommon characters of the two strings
Find and print the uncommon characters of the two given strings in sorted order. Here uncommon character means that either the character is present in one string or it is present in another string but not in both. The strings contain only lowercase characters and can contain duplicates. Source: Amazon Interview Experience | Set 355 (For 1 Year Expe
15+ min read
Reorder the given string to form a K-concatenated string
Given a string S and an integer K. The task is to form a string T such that the string T is a reordering of the string S in a way that it is a K-Concatenated-String. A string is said to be a K-Concatenated-String if it contains exactly K copies of some string.For example, the string "geekgeek" is a 2-Concatenated-String formed by concatenating 2 co
8 min read
Generate a string whose all K-size substrings can be concatenated to form the given string
Given a string str of size N and an integer K, the task is to generate a string whose substrings of size K can be concatenated to form the given string. Examples: Input: str = "abbaaa" K = 2 Output: abaa Explanation: All substring of size 2 of parent string "abaa" are "ab", "ba" and "aa". After concatenating all these substrings, the given string "
5 min read
Maximum number of times a given string needs to be concatenated to form a substring of another string
Given two strings S1 and S2 of length N and M respectively, the task is to find the maximum value of times the string S2 needs to be concatenated, such that it is a substring of the string S1. Examples: Input: S1 = "ababc", S2 = "ab"Output: 2Explanation: After concatenating S2 exactly twice, the string modifies to "abab". Therefore, string "abab" i
12 min read
Python - Convert Dictionary to Concatenated String
Sometimes, while working with Dictionaries, we can have a task in which we need to perform the conversion of converting dictionary to string, which is concatenated key-value pair. This can have application in domains in which we require to reduce storage space or require strings as target data. Let's discuss certain ways in which this task can be p
6 min read
Maximum Consecutive Zeroes in Concatenated Binary String
You are given a binary string str of length n. Suppose you create another string of size n * k by concatenating k copies of str together. What is the maximum size of a substring of the concatenated string consisting only of 0's? Given that k > 1. Examples: Input : str = "110010", k = 2 Output : 2 String becomes 110010110010 after two concatenati
8 min read
Print the final string when minimum value strings get concatenated in every operation
Given an array of strings and an array of integers where ith integer of the array corresponds to the value of the ith string present in the string array. Now pick two strings that have the smallest values in the integer array and sum up both the integers and concatenate the strings and add both the summed up integer to the integer array and the con
8 min read
three90RightbarBannerImg