Talentd
FleetCode

2023. Number of Pairs of Strings With Concatenation Equal to Target

Medium75.6% acceptance2 companies

Given an array of digit strings nums and a digit string target, return the number of pairs of indices (i, j) (where i != j) such that the concatenation of nums[i] + nums[j] equals target.

Example 1:

Input: nums = ["777","7","77","77"], target = "7777"
Output: 4
Explanation: Valid pairs are:
- (0, 1): "777" + "7"
- (1, 0): "7" + "777"
- (2, 3): "77" + "77"
- (3, 2): "77" + "77"

Example 2:

Input: nums = ["123","4","12","34"], target = "1234"
Output: 2
Explanation: Valid pairs are:
- (0, 1): "123" + "4"
- (2, 3): "12" + "34"

Example 3:

Input: nums = ["1","1","1"], target = "11"
Output: 6
Explanation: Valid pairs are:
- (0, 1): "1" + "1"
- (1, 0): "1" + "1"
- (0, 2): "1" + "1"
- (2, 0): "1" + "1"
- (1, 2): "1" + "1"
- (2, 1): "1" + "1"

Constraints:

  • 2 <= nums.length <= 100
  • 1 <= nums[i].length <= 100
  • 2 <= target.length <= 100
  • nums[i] and target consist of digits.
  • nums[i] and target do not have leading zeros.

Asked at 2 companies

Hints open one at a time

Hint 1
Try to concatenate every two different strings from the list.
Hint 2
Count the number of pairs with concatenation equals to target.
6:23
Still stuck?Read 4 approaches, or watch a walkthrough.
Open solution

Similar problems

Two SumEasy
Loading editor…
1class Solution:
2 def numOfPairs(self, nums: List[str], target: str) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
["777","7","77","77"]
"7777"
Input
["123","4","12","34"]
"1234"
Input
["1","1","1"]
"11"
Your code is saved on this device.Solution