Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Replacing Characters in a String Using Dictionary in Python
Next article icon

The goal is to combine two strings and identify the characters that appear in one string but not the other. These uncommon characters are then joined together in a specific order. In this article, we'll explore various methods to solve this problem using Python.

Using set symmetric difference 

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

Python
s1='aacdb's2='gafd'# Find and join uncommon charactersprint(''.join(set(s1)^set(s2)))

Output
fbgc

Explanation:

  • set(s1) and set(2): This converts the strings1 ands2 into a set of unique characters. This removes any duplicates.
  • ^ (Symmetric Difference):This operator performs a symmetric difference between two sets. It returns all elements that are in either set(s1) or set(s2), but not in both.
  • ''.join():ThisJoins the resulting set of characters back into a string without spaces.

Let's understand different methods to concatenated string with uncommon characters .

Using collections.Counter

collections.Countercount the occurrences of each character in the combined strings, then filters out characters that appear more than once.

Python
fromcollectionsimportCounters1='aacdb's2='gafd'f=Counter(s1+s2)# Filter and collect characters that appear only onceres=[chforchins1+s2iff[ch]==1]print(''.join(res))

Output
cbgf

Explanation:

  • Counter(str1 + str2): This counts all characters in the combined string str1 + str2.
  • List comprehension:This Filters out characters that appear more than once.
  • ''.join(result):This combines the filtered characters into a string.

Using Dictionary

This method uses adictionary to manually count the frequency of characters in the combined strings, then filters out those that appear only once.

Python
s1='aacdb's2='gafd'# Initialize an empty dictionaryf={}forchins1+s2:f[ch]=f.get(ch,0)+1# Filter characters that appear only onceres=[chforchins1+s2iff[ch]==1]print(''.join(res))

Output
cbgf

Explanation:

  • Frequency dictionary: It is used to count the occurrences of each character in the combined strings s1 + s2.
  • f[ch] == 1):This filters out characters that appear only once.
  • ''.join(res): This joins the filtered characters into a final string.

Using two pass filtering

This method identifies common characters between two strings and then filters them out in separate passes. It is simple but less efficient due to the extra overhead of processing the strings twice.

Python
s1='aacdb's2='gafd'c=set(s1)&set(s2)# Filter out common characters in two passesres=''.join([chforchins1ifchnotinc]+[chforchins2ifchnotinc])print(res)

Output
cbgf

Explanation:

  • set(s1) & set(s2): This finds the intersection of s1 and s2, i.e., the common characters.
  • Two-pass filtering:This filters the characters in both strings by checking if they are not in the common set.

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp