
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If Two String Arrays Are Equivalent in Python
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 −
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
Advertisements