Program to check whether two string arrays are equivalent or not in Python

PythonServer Side ProgrammingProgramming

Suppose we have two string type arrays word1 and word2, we have to check whether the two arrays represent the same string or not. We can say a string can be represented by an array if the elements in that array are concatenated in order forms the string.

So, if the input is like word1 = ["ko", "lka", "ta"] word2 = ["k", "olk", "at", "a"], then the output will be True as both are forming "kolkata".

To solve this, we will follow these steps −

  • s1:= blank string, s2:= blank string

  • for each string i in word1, do

    • s1 := s1 concatenate i

  • for each string i in word2, do

    • s2 := s2 + i

  • return true if s1 is same as s2, otherwise false

Example (Python)

Let us see the following implementation to get better understanding −

 Live Demo

def solve(word1, word2):
   s1=''
   s2=''
   for i in word1:
      s1+=i
   for i in word2:
      s2+=i
   return (s1==s2)

word1 = ["ko", "lka", "ta"]
word2 = ["k", "olk", "at", "a"]
print(solve(word1, word2))

Input

["ko", "lka", "ta"], ["k", "olk", "at", "a"]

Output

True
raja
Published on 17-May-2021 13:13:49
Advertisements