Problem
Given a pattern and a string s, find if s follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in s.
Algorithm
Use the dictionary to save the two list.
Code
class Solution:
def wordPattern(self, pattern: str, s: str) -> bool:
plen = len(pattern)
dict_p = dict()
dict_s = dict()
word = ""
index = 0
for c in 服务器托管网s:
if c >= 'a' and c 'z':
word += c
else:
if index >= plen:
服务器托管网 return False
pi = pattern[index]
if index plen and not pi in dict_p and not word in dict_s:
dict_p[pi] = word
dict_s[word] = pi
elif pi in dict_p and dict_p[pi] != word or word in dict_s and dict_s[word] != pi:
return False
index += 1
word = ""
if index >= plen:
return False
pi = pattern[index]
if index plen and not pi in dict_p and not word in dict_s:
dict_p[pi] = word
dict_s[word] = pi
elif pi in dict_p and dict_p[pi] != word or word in dict_s and dict_s[word] != pi:
return False
for i in range(index, plen):
if not pattern[i] in dict_p:
return False
return True
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
相关推荐: 前端学习笔记202310学习笔记第一百零七天-vue3-生命周期4
服务器托管,北京服务器托管网服务器托管网服务器托管,服务器租用 http://www.fwqtg.net 机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net相关推荐: 学正则表达式必用的工具正则可视化在线工具-更服务器托管网直观…