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

In this guide, you will get 2023. Number of Pairs of Strings With Concatenation Equal to Target LeetCode Solution with the best time and space complexity. The solution to Number of Pairs of Strings With Concatenation Equal to Target problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Number of Pairs of Strings With Concatenation Equal to Target solution in C++
  4. Number of Pairs of Strings With Concatenation Equal to Target solution in Java
  5. Number of Pairs of Strings With Concatenation Equal to Target solution in Python
  6. Additional Resources
2023. Number of Pairs of Strings With Concatenation Equal to Target LeetCode Solution image

Problem Statement of 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”

See also  1367. Linked List in Binary Tree LeetCode Solution

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.

Complexity Analysis

  • Time Complexity: O(|\texttt{nums}|)
  • Space Complexity: O(|\texttt{nums}|)

2023. Number of Pairs of Strings With Concatenation Equal to Target LeetCode Solution in C++

class Solution {
 public:
  int numOfPairs(vector<string>& nums, string target) {
    const int n = target.length();
    int ans = 0;
    unordered_map<string, int> count;

    for (const string& num : nums) {
      const int k = num.length();
      if (k >= n)
        continue;
      if (target.substr(0, k) == num)
        ans += count[target.substr(k)];
      if (target.substr(n - k) == num)
        ans += count[target.substr(0, n - k)];
      ++count[num];
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

2023. Number of Pairs of Strings With Concatenation Equal to Target LeetCode Solution in Java

class Solution {
  public int numOfPairs(String[] nums, String target) {
    final int n = target.length();
    int ans = 0;
    Map<String, Integer> count = new HashMap<>();

    for (final String num : nums) {
      final int k = num.length();
      if (k >= n)
        continue;
      if (target.substring(0, k).equals(num))
        ans += count.getOrDefault(target.substring(k), 0);
      if (target.substring(n - k).equals(num))
        ans += count.getOrDefault(target.substring(0, n - k), 0);
      count.merge(num, 1, Integer::sum);
    }

    return ans;
  }
}
// code provided by PROGIEZ

2023. Number of Pairs of Strings With Concatenation Equal to Target LeetCode Solution in Python

class Solution:
  def numOfPairs(self, nums: list[str], target: str) -> int:
    ans = 0
    count = collections.Counter()

    for num in nums:
      k = len(num)
      if target[:k] == num:
        ans += count[target[k:]]
      if target[-k:] == num:
        ans += count[target[:-k]]
      count[num] += 1

    return ans
# code by PROGIEZ

Additional Resources

See also  3217. Delete Nodes From Linked List Present in Array LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.