Given two Strings, separated by delim, check if both contain same characters.
Input : test_str1 = ‘e!e!k!s!g’, test_str2 = ‘g!e!e!k!s’, delim = ‘!’
Output : True
Explanation : Same characters, just diff. positions.
Input : test_str1 = ‘e!e!k!s’, test_str2 = ‘g!e!e!k!s’, delim = ‘!’
Output : False
Explanation : g missing in 1st String.
Method #1 : Using sorted() + split()
In this, we perform split using split(), and then perform task of sorting to get strings in order, post that strings are compared using the comparison operator.
The original string 1 is : e:e:k:s:g The original string 2 is : g:e:e:k:s Are strings similar : True
Method #2 : Using set() + split()
In this, instead of sort(), we convert strings to set(), to get ordering. This works only on unique character strings.
The original string 1 is : e:k:s:g The original string 2 is : g:e:k:s Are strings similar : True
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Combine similar characters in Python using Dictionary Get() Method
- Python | Convert list of strings and characters to list of characters
- Python | Strings with similar front and rear character
- Python | Kth index character similar Strings
- Python - Remove similar index elements in Strings
- Chaining comparison operators in Python
- Python | Data Comparison and Selection in Pandas
- Python | Excel File Comparison
- Python Object Comparison : "is" vs "=="
- Python | Tkinter ttk.Checkbutton and comparison with simple Checkbutton
- Python | Find Hotel Prices using Hotel price comparison API
- Python | Consecutive String Comparison
- Comparison of Python with Other Programming Languages
- Comparison between Lists and Array in Python
- Case-insensitive string comparison in Python
- String Comparison in Python
- Comparison of Java with other programming languages
- Python2 vs Python3 | Syntax and performance Comparison
- When to Use Django? Comparison with other Development Stacks
- Python program to print k characters then skip k characters in a string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.