Number of Pairs of Strings With Concatenation Equal to Target - Problem
You're given an array of digit strings nums and a target digit string. Your mission is to find how many pairs of strings from the array can be concatenated together to form the target string.
More specifically, you need to count the number of pairs of indices (i, j) where i != j such that concatenating nums[i] + nums[j] equals the target.
Example: If nums = ["777", "7", "77", "77"] and target = "7777", then nums[1] + nums[0] = "7" + "777" = "7777" forms one valid pair.
Note: The order matters! nums[i] + nums[j] is different from nums[j] + nums[i] unless both strings are identical.
Input & Output
example_1.py โ Basic Case
$
Input:
nums = ["777","7","77","77"], target = "7777"
โบ
Output:
4
๐ก Note:
Valid pairs: (1,0): "7"+"777", (0,1): "777"+"7", (2,1): "77"+"7"+"7" (invalid), actually (2,0) and (3,0) don't work. Let me recalculate: "7"+"777"="7777" โ, "777"+"7"="7777" โ. That's 2 pairs total.
example_2.py โ No Valid Pairs
$
Input:
nums = ["123","4","12","34"], target = "1234"
โบ
Output:
2
๐ก Note:
Valid pairs: (2,3): "12"+"34"="1234" โ, (0,1): "123"+"4"="1234" โ. Total of 2 pairs.
example_3.py โ Duplicate Strings
$
Input:
nums = ["1","1","1"], target = "11"
โบ
Output:
6
๐ก Note:
Each "1" can pair with every other "1". With 3 strings, we have 3ร2=6 ordered pairs: (0,1), (0,2), (1,0), (1,2), (2,0), (2,1).
Constraints
- 2 โค nums.length โค 100
- 1 โค nums[i].length, target.length โค 100
- nums[i] and target consist of digits only
- All strings contain only digits 0-9
Visualization
Tap to expand
Understanding the Visualization
1
Identify Pieces
Each string in the array is a puzzle piece
2
Find Complements
For each piece, find what other piece would complete the target
3
Count Matches
Count all valid piece combinations that form the complete target
Key Takeaway
๐ฏ Key Insight: Every valid concatenation corresponds to a unique way to split the target string into a prefix and suffix, both of which must exist in our input array.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code