String Array Equivalency - codepath/compsci_guides GitHub Wiki
TIP102 Unit 1 Session 2 Standard (Click for link to problem statements)
Problem Highlights
- 💡 Difficulty: Easy
- ⏰ Time to complete: 10 mins
- 🛠️ Topics: Arrays, Conditionals, Iteration
U-nderstand
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
- The function
are_equivalent()
should take two string arrays word1 and word2 and return True if the concatenated strings they represent are equal, and False otherwise.
HAPPY CASE
Input: word1 = ["bat", "man"], word2 = ["b", "atman"]
Expected Output: True
Input: word1 = ["cat", "wom", "an"], word2 = ["catwoman"]
Expected Output: True
EDGE CASE
Input: word1 = ["hello"], word2 = ["hello"]
Expected Output: True
P-lan
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Use string methods to concatenate the elements of both arrays and compare the resulting strings.
1. Define the function `are_equivalent(word1, word2)`.
2. Concatenate all elements in `word1` to form `concatenated_word1`.
3. Concatenate all elements in `word2` to form `concatenated_word2`.
4. Compare `concatenated_word1` and `concatenated_word2`.
5. Return the result of the comparison
⚠️ Common Mistakes
- Forgetting to handle empty arrays or using wrong concatenation methods.
I-mplement
Implement the code to solve the algorithm.
def are_equivalent(word1, word2):
# Concatenate all elements in word1 and word2
concatenated_word1 = ''.join(word1)
concatenated_word2 = ''.join(word2)
# Compare the concatenated strings
return concatenated_word1 == concatenated_word2