Home > OS >  Check if any character in one string appears in another
Check if any character in one string appears in another

Time:12-13

I have a string of text

hfHrpphHBppfTvmzgMmbLbgf

I have separated this string into two half's

hfHrpphHBppf,TvmzgMmbLbgf

I'd like to check if any of the characters in the first string, also appear in the second string, and would like to class lowercase and uppercase characters as separate (so if string 1 had a and string 2 had A this would not be a match).

and the above would return:

f

CodePudding user response:

split_text = ['hfHrpphHBppf', 'TvmzgMmbLbgf']

for char in split_text[0]:
    if char in split_text[1]:
        print(char)

There is probably a better way to do it, but this a quick and simple way to do what you want.

Edit:

split_text = ['hfHrpphHBppf', 'TvmzgMmbLbgf']
found_chars = []

for char in split_text[0]:
    if char in split_text[1] and char not in found_chars:
        found_chars.append(char)
        print(char)

There is almost certainly a better way of doing this, but this is a way of doing it with the answer I already gave

CodePudding user response:

You could use the "in" word.

something like this :

for i in range(len(word1) : 
   if word1[i] in word2 : 
      print(word[i]) 

Not optimal, but it should print you all the letter in common

CodePudding user response:

You can achieve this using set() and intersection

text = "hfHrpphHBppf,TvmzgMmbLbgf"
text = text.split(",")
print(set(text[0]).intersection(set(text[1])))

CodePudding user response:

str1 = "hfHrpphHBppfTvmzgMmbLbgf" str2 = "hfHrpphHBppf,TvmzgMmbLbgf"

if "hfHrpphHBppf" in str1: print("Yes") else: print("No")

CodePudding user response:

You can use list comprehension in order to check if letters from string a appears in string b.

a='hfHrpphHBppf'
b='TvmzgMmbLbgf'
c=[x for x in a if x in b]
print(' '.join(set(c)))

then output will be:

f

But you can use for,too. Like:

a='hfHrpphHBppf'
b='TvmzgMmbLbgf'
c=[]
for i in a:
  if i in b:
  c.append(i)
print(set(c))

CodePudding user response:

Here is a Python solution using a set to store and check for the characters in each string:

#Store the characters in the first string in a set
chars1 = set(string1)

# Create an empty set to store the matching characters
matching_chars = set()

# Loop through each character in the second string
for char in string2:
    # If the character is in the set of characters from the first string,
    # add it to the set of matching characters
    if char in chars1:
        matching_chars.add(char)

# Print the set of matching characters
print(matching_chars)

This solution has a time complexity of O(n), where n is the length of the longer string. This is because the set lookup time is constant and the loop only goes through each character in the second string once.

If you want to class lowercase and uppercase characters as separate, you can convert each string to lowercase or uppercase before storing the characters in the set. For example:

# Store the characters in the first string in a set, converting to lowercase
chars1 = set(string1.lower())

# Loop through each character in the second string, converting to lowercase
for char in string2.lower():
    ...

This will ensure that both a and A are considered the same character when checking for matches.

  • Related