2023. 连接后等于目标字符串的字符串对
大约 2 分钟
---
2023. 连接后等于目标字符串的字符串对
- 标签:数组、字符串
- 难度:中等
题目链接
题目大意
描述:给定一个数字字符串数组 nums 和一个数字字符串 target。
要求:返回 nums[i] + nums[j] (两个字符串连接,其中 i != j)结果等于 target 的下标 (i, j) 的数目。
说明:
- 。
- 。
- 。
nums[i]和target只包含数字。nums[i]和target不含有任何前导 。
示例:
- 示例 1:
输入:nums = ["777","7","77","77"], target = "7777"
输出:4
解释:符合要求的下标对包括:
- (0, 1):"777" + "7"
- (1, 0):"7" + "777"
- (2, 3):"77" + "77"
- (3, 2):"77" + "77"- 示例 2:
输入:nums = ["123","4","12","34"], target = "1234"
输出:2
解释:符合要求的下标对包括
- (0, 1):"123" + "4"
- (2, 3):"12" + "34"解题思路
思路 1:暴力枚举
- 双重循环遍历所有的
i和j,满足i != j并且nums[i] + nums[j] == target时,记入到答案数目中。 - 遍历完,返回答案数目。
思路 1:代码
class Solution:
def numOfPairs(self, nums: List[str], target: str) -> int:
res = 0
for i in range(len(nums)):
for j in range(len(nums)):
if i != j and nums[i] + nums[j] == target:
res += 1
return res思路 1:复杂度分析
- 时间复杂度:。
- 空间复杂度:。
思路 2:哈希表
- 使用哈希表记录字符串数组
nums中所有数字字符串的数量。 - 遍历哈希表中的键
num。 - 将
target根据num的长度分为前缀prefix和suffix。 - 如果
num等于prefix,则判断后缀suffix是否在哈希表中,如果在哈希表中,则说明prefix和suffix能够拼接为target。- 如果
num等于suffix,此时perfix == suffix,则答案数目累积为table[prefix] * (table[suffix] - 1)。 - 如果
num不等于suffix,则答案数目累积为table[prefix] * table[suffix]。
- 如果
- 最后输出答案数目。
思路 2:代码
class Solution:
def numOfPairs(self, nums: List[str], target: str) -> int:
res = 0
table = collections.defaultdict(int)
for num in nums:
table[num] += 1
for num in table:
size = len(num)
prefix, suffix = target[ :size], target[size: ]
if num == prefix and suffix in table:
if num == suffix:
res += table[prefix] * (table[suffix] - 1)
else:
res += table[prefix] * table[suffix]
return res思路 2:复杂度分析
- 时间复杂度:。
- 空间复杂度:。