Write Python Program to Find Uncommon Characters of the Two Strings

In this tutorial, we will write the Python program to find the uncommon character from the given two strings. The uncommon characters refer to that character is in one string or present in another string but not in both. The given strings are lowercase characters and can contain duplicates. Let's understand the following example.

Example -

To solve this problem, we will use the following approach.

Method - 1: Naïve Approach

In this approach, we will use the two loops to check the 1st string character present in the 2nd string and vice-versa.

Note - The output string must be in the sorted format.

Let's understand the following algorithm for the naïve approach.

Algorithm

  1. First, we will get the two strings str1 and str2 as input.
  2. Then, we define an empty list uncommon_chars to store the uncommon characters.
  3. Now, traverse str1 and check if a character is present in str2.
  4. Then, check if the character is not present in the str2 and not already added to the uncommon_chars, then add it to uncommon_chars and mark it as used.
  5. Now, traverse the str1 and check if a character present in the str2.
  6. If the character is not present in str1 and not already added to ans, then add it to ans and mark it as used.
  7. Sort the ans string in lexicographical order.
  8. If ans is empty, print -1. Otherwise, print the contents of ans.

Let's understand the below code.

Example -

Output:

Uncommon characters: dehrw

Explanation -

In the above code, we initialize an empty list of uncommon_chars to store the uncommon characters found. We then iterate over each character in str1 and check if it is not present in str2 and also not already present in uncommon_chars. If both conditions are satisfied, we add the character to uncommon_chars. Similarly, we iterate over each character in str2 and perform the same check.

Finally, we return the list of uncommon_chars that contains all the unique characters that are present in either str1 or str2, but not in both.

The time complexity of this approach is O(n2) since we are using nested loops. It may not be efficient for the larger string.

We can optimize the above solution. Let's understand another method.

Method - 2: Using Hashing

In this approach, we will define an empty dictionary and count the frequency of characters. Once we get the frequency of each character, we find the character whose frequency is equal to 1 and append it to the list. Let's understand the following example.

Example -

Output:

Uncommon characters: dehrw

Explanation -

In the above code, we use a dictionary char_freq to store the frequency of characters in both str1 and str2. We iterate over each character in str1 and update the frequency count in char_freq. Then, we iterate over each character in str2 and update the frequency count accordingly.

After counting the frequencies, we create an empty list uncommon_chars to store the uncommon characters. We iterate over the key-value pairs in char_freq and check if the frequency is 1. If so, it means the character is unique to either str1 or str2, and we add it to uncommon_chars.

Finally, we return uncommon_chars containing the uncommon characters found in the two strings.

This time complexity of the above code is O(n) where n is the total number of characters in both str1 and str2.

Method - 3: Map-based Technique

Let's understand the following example.

Example -

Output:

ailnopruy

Explanation -

In the above code, we define the function uncommon_chars() takes two string parameters a and b and returns a string containing the uncommon characters between the two strings.

Then we initialize the two arrays mp1 and mp2 of size 26. These arrays represent the occurrence of characters in the strings a and b, respectively. The arrays are initially filled with zeros.

The first loop iterates over each character char in string a and marks the occurrence of that character by setting the corresponding element in mp1 to 1. The mapping is done based on the character's ASCII value, subtracting the ASCII value of 'a' from it to get the corresponding index in the array.

Similarly, the second loop iterates over each character char in string b and marks the occurrence of that character by setting the corresponding element in mp2 to An empty string chars is initialized to store the uncommon characters found. The next loop iterates over the range of 26, representing the 26 characters of the English alphabet.

Within this loop, the condition mp1[i] ^ mp2[i] checks if the occurrence of the character at index i differs between mp1 and mp2. If the condition is true, it means the character is either present in a or b, but not both.

If the condition is satisfied, the character corresponding to the index i is appended to the chars string using the chr function and the ASCII value calculation.

After the loop, the code checks if chars is still an empty string. If it is, there were no uncommon characters found, so the function returns "-1". Otherwise, it returns the chars string containing the uncommon characters.

Finally, the function is called with the strings "capital" and "country", and the returned result is printed.

Method - 4: Using Set

In this approach, we will convert the given string into the set and find the symmetric difference between them. Let's understand the following example.

Example -

Output:

bclpr
bclpr

Explanation -

The above code converts the strings into sets, finds the symmetric difference between the sets to obtain the uncommon characters, and then sorts and joins the characters to form a sorted string of uncommon characters.






Latest Courses