Given an array of digit strings nums and a digit string target, return the number of pairs of indices(i, j)(wherei != j) such that the concatenation ofnums[i] + nums[j]equalstarget.
classSolution {
publicintnumOfPairs(String[] nums, String target) {
int ans = 0;
// Looping through all pairs of indices i, jfor (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums.length; j++) {
if (i != j) {
// Concatenating nums[i] and nums[j] and checking if it equals targetif ((nums[i]+ nums[j]).equals(target)) {
ans++;
}
}
}
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution:
defnumOfPairs(self, nums: List[str], target: str) -> int:
ans: int =0# Looping through all pairs of indices i, jfor i in range(len(nums)):
for j in range(len(nums)):
if i != j:
# Concatenating nums[i] and nums[j] and checking if it equals targetif nums[i] + nums[j] == target:
ans +=1return ans