Skip to content
Related Articles

Related Articles

Python Program to check if two sentences can be made the same by rearranging the words
  • Last Updated : 23 Feb, 2021

Given two strings S1 and S2 representing two sentences, the task is to check if two sentences can be made same by rearranging the words of any of the strings.

Examples:

Input: S1 = “please select a category”, S2 = “category a please select”
Output: Yes

Input: S1 = “hello world this is python language”, S2 = “python language is this modern world”       
Output: No
Explanation: The word “hello” does not exist in the second string.

Approach using sort() and split() built-in functions: Follow the steps to solve the problem:



Below is the implementation of the above approach:

Python3




# Python implementation
# of the above approach
  
# Function to check if two sentences
# can be made same by rearranging words
def ReArrangeStrings(string1, string2):
  
    # Stores the words of the
    # sentences in separate lists
    list1 = list(string1.split())
    list2 = list(string2.split())
  
    # Sort both the strings
    list1.sort()
    list2.sort()
  
    # If two lists are equal
    if(list1 == list2):
        return True
    else:
        return False
  
  
# Driver Code
  
# Input
S1 = "please select a category"
S2 = "category please a select"
  
# Function call to check if two sentences
# can be made same by rearranging words
if(ReArrangeStrings(S1, S2)):
    print("Yes")
else:
    print("No")
Output:
Yes

Time Complexity: O(N* M* log(N)), where M is the length of the longest strings.
Auxiliary Space: O(N)

Approach using Counter() and split() built-in Functions: Follow the steps to solve the problem:

  • Store the words of the strings S1 and S2 in separate lists, say list1[] and list2[], respectively.
  • Count the words present in the strings S1 and S2 using the Counter() function and store it in separate variables, say counter1 and counter2 respectively.
  • Check if counter1 is equal to counter2 or not. If found to be true, print “Yes”. Otherwise, print “No”.

Below is the implementation of the above approach:

Python3




# Python implementation
# of the above approach
  
# Import counter function
# from collections
from collections import Counter
  
  
# Function to check if two sentences
# can be made same by rearranging words
def ReArrange(S1, S2):
  
    # Store the words of the
    # strings in separate lists
    list1 = list(S1.split())
    list2 = list(S2.split())
  
    listcounter1 = Counter(list1)
    listcounter2 = Counter(list2)
  
    # If counter of both the
    # sentences are same
    if(listcounter1 == listcounter2):
        return True
    else:
        return False
  
  
# Driver Code
  
# Input
S1 = "please select a category"
S2 = "category please a select"
  
# Function call to check if two
# sentences can be made same
# by rearranging words
if(ReArrange(S1, S2)):
    print("Yes")
else:
    print("No")
Output:
Yes

Time Complexity: O(N)
Auxiliary Space: O(N)

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :