Python:【新】每日一题 3
本帖最后由 liuzhengyuan 于 2021-3-21 21:06 编辑专辑说明(积分规则)
今天的题目:
给定一种规律 pattern 和一个字符串 str ,判断 str 是否遵循 pattern 所表示的规律。
这里的 遵循 指完全匹配,例如, pattern 里的每个字母和字符串 str 中的每个非空单词之间存在着双向连接的对应规律。
请封装成一个函数,pattern,str 为参数
函数模板:
def wordPattern(pattern, s):
"""
:type pattern: str
:type s: str
:rtype: bool
"""
示例1:
输入: pattern = "abba", str = "dog cat cat dog"
输出: true
示例 2:
输入:pattern = "abba", str = "dog cat cat fish"
输出: false
示例 3:
输入: pattern = "aaaa", str = "dog cat cat dog"
输出: false
示例 4:
输入: pattern = "abba", str = "dog dog dog dog"
输出: false
限制:
你可以假设 pattern 只包含小写字母, str 包含了由单个空格分隔的小写字母。
欢迎大家来答题{:10_298:}
来源:力扣(LeetCode) 本帖最后由 洋洋痒 于 2021-3-21 23:12 编辑
def wordPattern(pattern, s):
if len(set(pattern))!=len(set(s.split())) or len(pattern)!=len(s.split()):
return False
L=s.split()
count=0
s=len(L)-len(set(L))
for i in range(len(L)):
count+=1
if count>s:
return True
for j in range(i+1,len(L)):
if L==L and pattern!=pattern:
return False
return True
print(wordPattern("abba","dog cat cat dog"))
print(wordPattern("abba","dog cat cat fish"))
print(wordPattern("aaaa","dog cat cat dog"))
print(wordPattern("aaaa","dog dog dog dog"))
我猜又会超时 def wordPattern(pattern, string):
str_map = dict()
string = string.split()
for i, p in enumerate(pattern):
if p in str_map.keys():
if string not in str_map:
return False
else:
str_map = ]
if len(set(pattern)) != len(set(string)):
return False
return True def wordPattern(pattern, s):
pattern_list = list(pattern)
s_list = s.split(' ')
length_s = len(s_list)
length_p = len(pattern_list)
if length_s != length_p:
return False
check_list = []
for each in range(length_p):
check_list.append(, each])
check_list.sort()
for each in range(len(check_list) - 1):
if check_list == check_list:
if s_list] == s_list]:
pass
else:
return False
return True
print(wordPattern("abba", "dog cat cat dog"))
print(wordPattern("abba", "dog cat cat fish"))
print(wordPattern("aaaa", "dog cat cat dog"))
print(wordPattern("aaaa", "dog dog dog dog"))
单层 for 循环应该会快一点?{:10_245:} 我一直搞不懂这种题,是不能用函数么,还是匹配的功能要自己写 本帖最后由 qq1151985918 于 2021-3-22 09:27 编辑
def wordPattern(pattern, s):
pList,sList = list(pattern),s.split()
if len(pList) != len(sList) or len(set(pList)) != len(set(sList)):
return False
Dict = {}
for i in range(len(pList)):
if pList not in Dict.keys():
Dict] = sList
else:
if Dict] != sList:
return False
return True def wordPattern(keys, s):
values = s.split(' ')
if len(keys) != len(values):
return False
dic1 = {}
for i in range(len(keys)):
if(keys not in dic1.keys()):
if(values in dic1.values()):
return False
dic1] = values
else:
if(dic1.get(keys) != values):
return False
return True
pass
pattern = 'abbba'
str1 = 'dog cat cat cat dog'
print(wordPattern(pattern, str1))
def wordPattern(pattern: str, s:str):
"""
:type pattern: str
:type s: str
:rtype: bool
"""
strs = s.split(" ")
if len(pattern) != len(strs): return False
D = dict()
for p, ss in zip(pattern, strs):
# 根据规则p,去映射字典取字符串
r = D.get(p, None)
# 如果是None,说明当前规则没有录入
if r is None:
D = ss
# 如果有值,且和当前字符对不上,返回False
elif r != ss:
return False
return True 柿子饼同学 发表于 2021-3-22 07:43
我一直搞不懂这种题,是不能用函数么,还是匹配的功能要自己写
封装成函数更方便测试代码 -------------------------------------分割线-------------------------------
之后答题不会有奖励 洋洋痒 发表于 2021-3-21 22:14
我猜又会超时
输入:
"aba"
"dog cat cat"
结果错误 小仙 发表于 2021-3-21 22:41
输入:
"aba"
"cat cat cat dog"
解答错误 Daniel_Zhang 发表于 2021-3-21 23:25
单层 for 循环应该会快一点?
输入:
"abba"
"dog dog dog dog"
输出:
true
预期结果:
false Stubborn 发表于 2021-3-22 17:04
输入:
"abba"
"dog dog dog dog"
输出:
true
预期结果:
false liuzhengyuan 发表于 2021-3-24 17:06
输入:
"abba"
"dog dog dog dog"
def wordPattern(pattern, s):
pattern_list = list(pattern)
s_list = s.split(' ')
length_s = len(s_list)
length_p = len(pattern_list)
if length_s != length_p:
return False
check_list = []
for each in range(length_p):
check_list.append(, each])
check_list.sort()
print(check_list)
for each in range(len(check_list) - 1):
if check_list == check_list:
if s_list] == s_list]:
pass
else:
return False
else:
if s_list] == s_list]:
return False
return True
这样应该就可以了吧,漏了一种情况{:10_278:}
页:
[1]