Python Program to check if two sentences can be made the same by rearranging the words

In this problem, we need to check whether we can make two strings equal by rearranging the words of the string. We will learn three different approaches to solving the problem.

In the first approach, we will use a dictionary. In the second approach, we will use the sort() method, and in the third approach, we will use the counter() constructor, which is used to count the hashable objects in the python.

Problem statement – We have given a str1 and str2 containing sentences. We need to check whether we can make both strings equal by rearranging the words of the strings. Print ‘Yes’ or ‘No’ based on whether we can make strings equal by rearranging words.

Sample examples

Input– Str1 = "welcome to the tutorialspoint", Str2 = "tutorialspoint to welcome the"

Output– ‘Yes’

Explanation– As both strings contain equal and same words, we can make both strings equal by rearranging words.

Input– Str1 = ‘a b c d’ Str2 = ‘b c d e’

Output– ‘No’

Explanation–Str1 doesn’t contain ‘e’, but Str2 contains it, so we can’t make them equal by rearranging words.

Input– Str1 = ‘Hello’ Str2 = ‘Hello’

Output– ‘Yes’

Explanation– As both strings are already equal, we get ‘Yes’ in the output.

Approach 1

In this approach, we will use Python dictionaries to store the total number of occurrences of each word in both strings. After that, we will compare both dictionaries to get the output.

Algorithm

• Use the split() method and list() constructor to convert a string into a list of words.

• Initialize the words1 and words2 with empty dictionaries.

• Use the get() method to value stored for the current word key, and update the new value by adding 1

• Return the results of words1 == words2. If words1 and words2 are equal, both strings contain the same number of equal words

Example

# function to check if two sentences can be made the same by rearranging words
def reArrangeWords(string1, string2):
# Using the split() function to break the sentence into a list of words
list1 = list(string1.split())
list2 = list(string2.split())
# defining empty dictionaries to store the frequency of each word
words1 = {}
words2 = {}
# using get() to store the frequency of each word in a dictionary
for word in list1:
words1[word] = words1.get(word, 0) + 1
for word in list2:
words2[word] = words2.get(word, 0) + 1
# If both the dictionaries are equal, then the sentences can be made the same by rearranging words
return words1 == words2
# Input
Str1 = "welcome to the tutorialspoint"
Str2 = "tutorialspoint to welcome the"
# Function call
if (reArrangeWords(Str1, Str2)):
print("Yes, both the sentences can be made same by rearranging words")
else:
print("No, both the sentences cannot be made same by rearranging words")


Output

Yes, both the sentences can be made same by rearranging words


Time complexity – O(N), as we traverse both strings.

Space complexity – O(N), as we use dictionaries to store the words and their occurrence in the particular string.

Approach 2

We will use the sort() method to sort the list in this approach. After that, we will compare both lists, and if they are equal, we can rearrange words to make both strings the same.

Algorithm

• Use the split() method to convert strings to lists

• Use the sort() method to sort the list1 and list2 one after another

• If list1 and list2 are equal, return true. Else return false.

Example

def reArrangeWords(string1, string2):
# Using the split() function to break the sentence into a list of words
list1 = list(string1.split())
list2 = list(string2.split())
# sort the list of words
list1.sort()
list2.sort()
# If all words are the same, then the sentences can be made the same by rearranging words
if (list1 == list2):
return True
else:
return False

Str1 = "welcome to the tutorialspoint"
Str2 = "tutorialspoint to welcome the"
# Function call
if (reArrangeWords(Str1, Str2)):
print("Yes, both the sentences can be made same by rearranging words")
else:
print("No, both the sentences cannot be made same by rearranging words")


Output

Yes, both the sentences can be made same by rearranging words


Time complexity – O(N*logN), as we sort both lists.

Space complexity – O(N), as we store the string words in the list.

Approach 3

In this approach, we will use the counter() constructor to create a map containing words as a key and the total number of their occurrences in the sentence. If we get the same object for both strings by using the counter() constructor, we can rearrange the words of strings to make them equal.

Algorithm

• Split the string to convert it into the list of words.

• Use the counter() constructor and pass the list as a parameter. Also, the store returned objects in the counter1 and counter2 variables for the list1 and list2, respectively.

• If counter1 and counter2 are the same, return true. Else return false.

Example

from collections import Counter
def reArrangeWords(string1, string2):
# Using the split() function to break the sentence into a list of words
list1 = list(string1.split())
list2 = list(string2.split())
# sort the list of words
Counter1 = Counter(list1)
Counter2 = Counter(list2)
# If both sentences have the same set of words, return true. Else return false.
if (Counter1 == Counter2):
return True
else:
return False
# Input
Str1 = "Hey how are you"
Str2 = "Hey how you are"
# Function call
if (reArrangeWords(Str1, Str2)):
print("Yes, both the sentences can be made same by rearranging words")
else:
print("No, both the sentences cannot be made same by rearranging words")


Output

Yes, both the sentences can be made same by rearranging words


Time complexity – O(N) as we use the split() method and counter() constructor.

Space complexity – O(N) as we store a string into a list after splitting.

We have seen three approaches to solving the given problem. The second approach using the sort() method, has more time and space complexity than the first and third approaches, as we need to sort the list. The first and third approaches are optimized and have the same time and space complexity. Also, the code of the third approach is more readable than the first approach’s code.

Updated on: 18-Aug-2023

52 Views