Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 <= 1001 <= nums[i].length <= 1002 <= target.length <= 100nums[i] and target consist of digits.nums[i] and target do not have leading zeros.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
["777","7","77","77"] "7777"
["123","4","12","34"] "1234"
["1","1","1"] "11"
two-sum)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2023: Number of Pairs of Strings With Concatenation Equal to Target
class Solution {
public int numOfPairs(String[] nums, String target) {
int n = nums.length;
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i != j && target.equals(nums[i] + nums[j])) {
++ans;
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #2023: Number of Pairs of Strings With Concatenation Equal to Target
func numOfPairs(nums []string, target string) (ans int) {
for i, a := range nums {
for j, b := range nums {
if i != j && a+b == target {
ans++
}
}
}
return ans
}
# Accepted solution for LeetCode #2023: Number of Pairs of Strings With Concatenation Equal to Target
class Solution:
def numOfPairs(self, nums: List[str], target: str) -> int:
n = len(nums)
return sum(
i != j and nums[i] + nums[j] == target for i in range(n) for j in range(n)
)
// Accepted solution for LeetCode #2023: Number of Pairs of Strings With Concatenation Equal to Target
/**
* [2023] Number of Pairs of Strings With Concatenation Equal to Target
*
* 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.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/number-of-pairs-of-strings-with-concatenation-equal-to-target/
// discuss: https://leetcode.com/problems/number-of-pairs-of-strings-with-concatenation-equal-to-target/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn num_of_pairs(nums: Vec<String>, target: String) -> i32 {
nums.iter()
.enumerate()
.flat_map(|(i, a)| {
nums.iter()
.enumerate()
.filter(move |(j, _)| i != *j)
.map(move |(_, b)| (a, b))
})
.filter(|&(a, b)| target.strip_prefix(a).is_some_and(|suffix| suffix == b))
.count() as i32
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2023_example_1() {
let nums = vec_string!["777", "7", "77", "77"];
let target = "7777".to_string();
let result = 4;
assert_eq!(Solution::num_of_pairs(nums, target), result);
}
#[test]
fn test_2023_example_2() {
let nums = vec_string!["123", "4", "12", "34"];
let target = "1234".to_string();
let result = 2;
assert_eq!(Solution::num_of_pairs(nums, target), result);
}
#[test]
fn test_2023_example_3() {
let nums = vec_string!["1", "1", "1"];
let target = "11".to_string();
let result = 6;
assert_eq!(Solution::num_of_pairs(nums, target), result);
}
}
// Accepted solution for LeetCode #2023: Number of Pairs of Strings With Concatenation Equal to Target
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #2023: Number of Pairs of Strings With Concatenation Equal to Target
// class Solution {
// public int numOfPairs(String[] nums, String target) {
// int n = nums.length;
// int ans = 0;
// for (int i = 0; i < n; ++i) {
// for (int j = 0; j < n; ++j) {
// if (i != j && target.equals(nums[i] + nums[j])) {
// ++ans;
// }
// }
// }
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.