鱼C论坛

 找回密码
 立即注册
查看: 3288|回复: 14

[已解决]Python:【新】每日一题 3

[复制链接]
发表于 2021-3-21 20:57:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 liuzhengyuan 于 2021-3-21 21:06 编辑

专辑说明(积分规则)

今天的题目:


给定一种规律 pattern 和一个字符串 str ,判断 str 是否遵循 pattern 所表示的规律。

这里的 遵循 指完全匹配,例如, pattern 里的每个字母和字符串 str 中的每个非空单词之间存在着双向连接的对应规律。

请封装成一个函数,pattern,str 为参数

函数模板:

  1. def wordPattern(pattern, s):
  2.     """
  3.     :type pattern: str
  4.     :type s: str
  5.     :rtype: bool
  6.     """
复制代码

示例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 包含了由单个空格分隔的小写字母。   

欢迎大家来答题


来源:力扣(LeetCode)
最佳答案
2021-3-22 12:29:26
  1. def wordPattern(keys, s):

  2.     values = s.split(' ')
  3.    
  4.     if len(keys) != len(values):
  5.         return False

  6.     dic1 = {}

  7.     for i in range(len(keys)):
  8.         if(keys[i] not in dic1.keys()):
  9.             if(values[i] in dic1.values()):
  10.                 return False
  11.             dic1[keys[i]] = values[i]
  12.             
  13.         else:
  14.             if(dic1.get(keys[i]) != values[i]):
  15.                 return False

  16.     return True
  17.    
  18.     pass


  19. pattern = 'abbba'
  20. str1 = 'dog cat cat cat dog'
  21. print(wordPattern(pattern, str1))
复制代码

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-3-21 22:14:53 | 显示全部楼层
本帖最后由 洋洋痒 于 2021-3-21 23:12 编辑
  1. def wordPattern(pattern, s):
  2.     if len(set(pattern))!=len(set(s.split())) or len(pattern)!=len(s.split()):
  3.         return False
  4.     L=s.split()
  5.     count=0
  6.     s=len(L)-len(set(L))
  7.     for i in range(len(L)):
  8.         count+=1
  9.         if count>s:
  10.             return True
  11.         for j in range(i+1,len(L)):
  12.             if L[j]==L[i] and pattern[j]!=pattern[i]:
  13.                 return False
  14.     return True
  15. print(wordPattern("abba","dog cat cat dog"))
  16. print(wordPattern("abba","dog cat cat fish"))
  17. print(wordPattern("aaaa","dog cat cat dog"))
  18. print(wordPattern("aaaa","dog dog dog dog"))
复制代码



我猜又会超时

评分

参与人数 1荣誉 +2 鱼币 +2 收起 理由
liuzhengyuan + 2 + 2 出错了

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-21 22:41:00 | 显示全部楼层
  1. def wordPattern(pattern, string):
  2.   str_map = dict()
  3.   string = string.split()
  4.   for i, p in enumerate(pattern):
  5.     if p in str_map.keys():
  6.       if string[i] not in str_map[p]:
  7.         return False
  8.     else:
  9.       str_map[p] = [string[i]]
  10.   if len(set(pattern)) != len(set(string)):
  11.     return False
  12.   return True
复制代码

评分

参与人数 1荣誉 +2 鱼币 +2 收起 理由
liuzhengyuan + 2 + 2

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-21 23:25:29 | 显示全部楼层
  1. def wordPattern(pattern, s):
  2.     pattern_list = list(pattern)
  3.     s_list = s.split(' ')
  4.     length_s = len(s_list)
  5.     length_p = len(pattern_list)
  6.     if length_s != length_p:
  7.         return False
  8.     check_list = []
  9.     for each in range(length_p):
  10.         check_list.append([pattern_list[each], each])
  11.     check_list.sort()

  12.     for each in range(len(check_list) - 1):
  13.         if check_list[each][0] == check_list[each + 1][0]:
  14.             if s_list[check_list[each][1]] == s_list[check_list[each + 1][1]]:
  15.                 pass
  16.             else:
  17.                 return False
  18.     return True


  19. print(wordPattern("abba", "dog cat cat dog"))
  20. print(wordPattern("abba", "dog cat cat fish"))
  21. print(wordPattern("aaaa", "dog cat cat dog"))
  22. print(wordPattern("aaaa", "dog dog dog dog"))
复制代码


单层 for 循环应该会快一点?

评分

参与人数 1荣誉 +2 鱼币 +2 收起 理由
liuzhengyuan + 2 + 2

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-22 07:43:58 | 显示全部楼层
我一直搞不懂这种题,是不能用函数么,还是匹配的功能要自己写
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-22 09:03:20 | 显示全部楼层
本帖最后由 qq1151985918 于 2021-3-22 09:27 编辑
  1. def wordPattern(pattern, s):
  2.     pList,sList = list(pattern),s.split()
  3.     if len(pList) != len(sList) or len(set(pList)) != len(set(sList)):
  4.         return False
  5.     Dict = {}
  6.     for i in range(len(pList)):
  7.         if pList[i] not in Dict.keys():
  8.             Dict[pList[i]] = sList[i]
  9.         else:
  10.             if Dict[pList[i]] != sList[i]:
  11.                 return False
  12.     return True
复制代码

评分

参与人数 1荣誉 +4 鱼币 +4 收起 理由
liuzhengyuan + 4 + 4 24ms 13.3mb

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-22 12:29:26 | 显示全部楼层    本楼为最佳答案   
  1. def wordPattern(keys, s):

  2.     values = s.split(' ')
  3.    
  4.     if len(keys) != len(values):
  5.         return False

  6.     dic1 = {}

  7.     for i in range(len(keys)):
  8.         if(keys[i] not in dic1.keys()):
  9.             if(values[i] in dic1.values()):
  10.                 return False
  11.             dic1[keys[i]] = values[i]
  12.             
  13.         else:
  14.             if(dic1.get(keys[i]) != values[i]):
  15.                 return False

  16.     return True
  17.    
  18.     pass


  19. pattern = 'abbba'
  20. str1 = 'dog cat cat cat dog'
  21. print(wordPattern(pattern, str1))
复制代码

评分

参与人数 1荣誉 +4 鱼币 +4 收起 理由
liuzhengyuan + 4 + 4 12ms 12.9mb

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-22 17:04:02 | 显示全部楼层
  1. def wordPattern(pattern: str, s:str):
  2.     """
  3.     :type pattern: str
  4.     :type s: str
  5.     :rtype: bool
  6.     """
  7.     strs = s.split(" ")
  8.     if len(pattern) != len(strs): return False
  9.     D = dict()
  10.     for p, ss in zip(pattern, strs):
  11.         # 根据规则p,去映射字典取字符串
  12.         r = D.get(p, None)
  13.         # 如果是None,说明当前规则没有录入
  14.         if r is None:
  15.             D[p] = ss
  16.         # 如果有值,且和当前字符对不上,返回False
  17.         elif r != ss:
  18.             return False
  19.     return True
复制代码

评分

参与人数 1荣誉 +2 鱼币 +2 收起 理由
liuzhengyuan + 2 + 2

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-23 19:10:10 | 显示全部楼层
柿子饼同学 发表于 2021-3-22 07:43
我一直搞不懂这种题,是不能用函数么,还是匹配的功能要自己写

封装成函数更方便测试代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-23 20:50:52 | 显示全部楼层
-------------------------------------分割线-------------------------------
之后答题不会有奖励
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-24 16:57:41 | 显示全部楼层

输入:
"aba"
"dog cat cat"
结果错误
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-24 17:05:26 | 显示全部楼层

输入:
"aba"
"cat cat cat dog"
解答错误
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-24 17:06:48 | 显示全部楼层
Daniel_Zhang 发表于 2021-3-21 23:25
单层 for 循环应该会快一点?

输入:
"abba"
"dog dog dog dog"
输出:
true
预期结果:
false
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-24 17:13:18 | 显示全部楼层

输入:
"abba"
"dog dog dog dog"
输出:
true
预期结果:
false
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-24 17:32:47 | 显示全部楼层
liuzhengyuan 发表于 2021-3-24 17:06
输入:
"abba"
"dog dog dog dog"
  1. def wordPattern(pattern, s):
  2.     pattern_list = list(pattern)
  3.     s_list = s.split(' ')
  4.     length_s = len(s_list)
  5.     length_p = len(pattern_list)
  6.     if length_s != length_p:
  7.         return False
  8.     check_list = []
  9.     for each in range(length_p):
  10.         check_list.append([pattern_list[each], each])
  11.     check_list.sort()
  12.     print(check_list)

  13.     for each in range(len(check_list) - 1):
  14.         if check_list[each][0] == check_list[each + 1][0]:
  15.             if s_list[check_list[each][1]] == s_list[check_list[each + 1][1]]:
  16.                 pass
  17.             else:
  18.                 return False
  19.         else:
  20.             if s_list[check_list[each][1]] == s_list[check_list[each+1][1]]:
  21.                 return False
  22.     return True
复制代码


这样应该就可以了吧,漏了一种情况
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-20 05:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表