Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String Comparing
#10
Hey! Friends After Some mind blowing tricks on google i approach with these solution!
# Function to print common characters of two Strings 
# in alphabetical order 
from collections import Counter 

def common(str1,str2): 
	
	# convert both strings into counter dictionary 
	dict1 = Counter(str1) 
	dict2 = Counter(str2) 

	# take intersection of these dictionaries 
	commonDict = dict1 & dict2 

	if len(commonDict) == 0: 
		print -1
		return

	# get a list of common elements 
	commonChars = list(commonDict.elements()) 

	# sort list in ascending order to print resultant 
	# string on alphabetical order 
	commonChars = sorted(commonChars) 

	# join characters without space to produce 
	# resultant string 
	print(''.join(commonChars)) 

# Driver program 
if __name__ == "__main__": 
	str1 = input('Please Type The First String:\n')
	str2 = input('Please Type The Second String:\n')
	common(str1, str2) 
Reply


Messages In This Thread
String Comparing - by Harshil - Aug-02-2020, 12:49 PM
RE: String Comparing - by Yoriz - Aug-02-2020, 01:01 PM
RE: String Comparing - by ibreeden - Aug-02-2020, 01:20 PM
RE: String Comparing - by Harshil - Aug-03-2020, 04:06 PM
RE: String Comparing - by ibreeden - Aug-04-2020, 05:47 PM
RE: String Comparing - by deanhystad - Aug-04-2020, 05:55 PM
RE: String Comparing - by Donovan - Aug-05-2020, 12:44 AM
RE: String Comparing - by Harshil - Aug-05-2020, 12:50 PM
RE: String Comparing - by buran - Aug-05-2020, 12:56 PM
RE: String Comparing - by Harshil - Aug-05-2020, 12:59 PM
RE: String Comparing - by deanhystad - Aug-05-2020, 02:14 PM
RE: String Comparing - by Harshil - Aug-05-2020, 05:09 PM
RE: String Comparing - by deanhystad - Aug-05-2020, 05:25 PM
RE: String Comparing - by Harshil - Aug-05-2020, 05:37 PM
RE: String Comparing - by deanhystad - Aug-05-2020, 05:49 PM
RE: String Comparing - by Harshil - Aug-05-2020, 05:50 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Comparing Dataframe to String? RockBlok 2 482 Nov-24-2023, 04:55 PM
Last Post: RockBlok
  Create a new list by comparing values in a list and string DBS 2 3,614 Jan-14-2017, 07:59 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020