LeetCode-in-Java

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

Medium

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:

Example 2:

Input: nums = [“123”,”4”,”12”,”34”], target = “1234”

Output: 2

Explanation: Valid pairs are:

Example 3:

Input: nums = [“1”,”1”,”1”], target = “11”

Output: 6

Explanation: Valid pairs are:

Constraints:

Solution

public class Solution {
    public int numOfPairs(String[] nums, String target) {
        int ans = 0;
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < nums.length; j++) {
                if (i != j) {
                    String con = nums[i] + nums[j];
                    if (con.equals(target)) {
                        ans++;
                    }
                }
            }
        }
        return ans;
    }
}