【发布时间】:2022-11-04 00:04:16
【问题描述】:
有一组输入字符串和一组查询字符串。对于每个查询字符串,确定它在输入字符串列表中出现的次数。返回结果数组。 例子:- 字符串 = ['ab','ab','abc'] 查询 = ['ab', 'abc','bc'] “ab”有 2 个实例,“abc”有 1 个,“bc”有 0 个。对于每个查询,将一个元素添加到返回数组。 结果 = [2,1,0]
功能说明
在下面的编辑器中完成函数matchingStrings。该函数必须返回一个整数数组,表示字符串中每个查询字符串的出现频率。
matchStrings 有以下参数:
string strings[n] - 要搜索的字符串数组 字符串查询[q] - 查询字符串数组 退货
int[q]:每个查询的结果数组
约束:
1 <= 长度(字符串)<= 1000,
1 <=len(查询)<= 1000 1 <= 字符串[i] <= 20,
1<=查询[i]<= 20
这是我的代码。它在示例测试用例上成功运行,但在 10/13 测试用例上失败。
#Code in python
def matchingStrings(strings, queries):
#first few lines satisfies the constraints
if len(strings) >= 1 and len(strings)<= 1000:
if len(queries)>= 1 and len(strings)<= 1000:
count_arr = {} # creating a dict to save each query count
for query in queries:
if len(query)>= 1 and len(query)<= 20:
count_arr[query] = 0
for string in strings:
if len(string)>= 1 and len(string)<= 20:
if query == string.strip():
count_arr[query] = count_arr[query] + 1
return list(count_arr.values())
【问题讨论】:
标签: python sparse-matrix