Leetcode 290. Word Pattern

这篇具有很好参考价值的文章主要介绍了Leetcode 290. Word Pattern。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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.文章来源地址https://www.toymoban.com/news/detail-725447.html

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

到了这里,关于Leetcode 290. Word Pattern的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LeetCode --- 1732. Find the Highest Altitude 解题报告

    There is a biker going on a road trip. The road trip consists of  n + 1  points at different altitudes. The biker starts his trip on point  0  with altitude equal  0 . You are given an integer array  gain  of length  n  where  gain[i]  is the  net gain in altitude  between points  i ​​​​​​ and  i + 1  for all ( 0 = i n) . Return 

    2024年02月02日
    浏览(26)
  • LeetCode --- 1710. Maximum Units on a Truck 解题报告

    You are assigned to put some amount of boxes onto  one truck . You are given a 2D array  boxTypes , where  boxTypes[i] = [numberOfBoxesi, numberOfUnitsPerBoxi] : numberOfBoxesi  is the number of boxes of type  i . numberOfUnitsPerBoxi  is the number of units in each box of the type  i . You are also given an integer  truckSize , which is the  maximu

    2023年04月18日
    浏览(30)
  • LeetCode --- 1903. Largest Odd Number in String 解题报告

    You are given a string  num , representing a large integer. Return  the  largest-valued odd  integer (as a string) that is a  non-empty substring  of  num , or an empty string  \\\"\\\"  if no odd integer exists . A  substring  is a contiguous sequence of characters within a string. Example 1: Example 2: Example 3:

    2024年02月10日
    浏览(23)
  • LeetCode --- 1863. Sum of All Subset XOR Totals 解题报告

    The  XOR total  of an array is defined as the bitwise  XOR  of  all its elements , or  0  if the array is  empty . For example, the  XOR total  of the array  [2,5,6]  is  2 XOR 5 XOR 6 = 1 . Given an array  nums , return  the  sum  of all  XOR totals  for every  subset  of  nums .  Note:  Subsets with the  same  elements should be c

    2024年02月15日
    浏览(40)
  • LeetCode --- 1971. Find if Path Exists in Graph 解题报告

    There is a  bi-directional  graph with  n  vertices, where each vertex is labeled from  0  to  n - 1  ( inclusive ). The edges in the graph are represented as a 2D integer array  edges , where each  edges[i] = [ui, vi]  denotes a bi-directional edge between vertex  ui  and vertex  vi . Every vertex pair is connected by  at most one  edge, and

    2024年02月07日
    浏览(32)
  • LeetCode --- 1869. Longer Contiguous Segments of Ones than Zeros 解题报告

    Given a binary string  s , return  true  if the  longest  contiguous segment of  1 \\\' s is  strictly longer  than the  longest  contiguous segment of  0 \\\' s in  s , or return  false  otherwise . For example, in  s = \\\" 11 01 000 10\\\"  the longest continuous segment of  1 s has length  2 , and the longest continuous segment of  0 s has length 

    2024年02月15日
    浏览(25)
  • LeetCode --- 1790. Check if One String Swap Can Make Strings Equal 解题报告

    You are given two strings  s1  and  s2  of equal length. A  string swap  is an operation where you choose two indices in a string (not necessarily different) and swap the characters at these indices. Return  true   if it is possible to make both strings equal by performing  at most one string swap  on  exactly one  of the strings.  Otherwise, re

    2024年02月10日
    浏览(42)
  • 【LeetCode】 双指针,快慢指针解题

    1.删除有序数组中的重复项 2.移除元素

    2024年02月11日
    浏览(44)
  • leetcode 122双周赛 解题思路+代码

    本人水平有限,只做出3道,最后1道放弃。 给你一个长度为 n 的整数数组 nums 。 一个数组的 代价 是它的 第一个 元素。比方说,[1,2,3] 的代价是 1 ,[3,4,1] 的代价是 3 。 你需要将 nums 分成 3 个 连续且没有交集 的子数组。 请你返回这些子数组的 最小 代价 总和 。 示例 1: 输

    2024年02月20日
    浏览(35)
  • leetcode解题思路分析(一百四十八)1289 - 1296 题

    下降路径最小和 II 给你一个 n x n 整数矩阵 grid ,请你返回 非零偏移下降路径 数字和的最小值。非零偏移下降路径 定义为:从 grid 数组中的每一行选择一个数字,且按顺序选出来的数字中,相邻数字不在原数组的同一列。 f[i][j] 表示从数组的前i行中的每一行选择一个数字,

    2024年02月09日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包