zoukankan      html  css  js  c++  java
  • 算法43----字符串【同模式】

    一、题目:同构字符串

    给定两个字符串 和 t,判断它们是否是同构的。

    如果 中的字符可以被替换得到 ,那么这两个字符串是同构的。

    所有出现的字符都必须用另一个字符替换,同时保留字符的顺序。两个字符不能映射到同一个字符上,但字符可以映射自己本身。

    示例 1:

    输入: s = "egg", t = "add"
    输出: true
    

    示例 2:

    输入: s = "foo", t = "bar"
    输出: false

    示例 3:

    输入: s = "paper", t = "title"
    输出: true

    说明:
    你可以假设 t 具有相同的长度。

    代码:

    def isIsomorphic(self, s, t):
        return len(set(zip(s,t))) == len(set(s)) == len(set(t))

    二、题目:单词模式:

    给定一种 pattern(模式) 和一个字符串 str ,判断 str 是否遵循相同的模式。

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

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

    代码:

        def wordPattern(self, pattern, str):
            """
            :type pattern: str
            :type str: str
            :rtype: bool
            """
            a = str.split(" ")
            if len(pattern) != len(a):
                return False
            return len(set(zip(a,pattern))) == len(set(pattern)) == len(set(a))

     三、题目:查找和替换模式

    你有一个单词列表 words 和一个模式  pattern,你想知道 words 中的哪些单词与模式匹配。

    如果存在字母的排列 p ,使得将模式中的每个字母 x 替换为 p(x) 之后,我们就得到了所需的单词,那么单词与模式是匹配的。

    (回想一下,字母的排列是从字母到字母的双射:每个字母映射到另一个字母,没有两个字母映射到同一个字母。)

    返回 words 中与给定模式匹配的单词列表。

    你可以按任何顺序返回答案。

    示例:

    输入:words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
    输出:["mee","aqq"]
    解释:
    "mee" 与模式匹配,因为存在排列 {a -> m, b -> e, ...}。
    "ccc" 与模式不匹配,因为 {a -> c, b -> c, ...} 不是排列。
    因为 a 和 b 映射到同一个字母。

    提示:

    • 1 <= words.length <= 50
    • 1 <= pattern.length = words[i].length <= 20

    思路:时间O(n2)

    对每个单词判断其与相应模式是否匹配,匹配则输出。

    判断是否匹配函数strMatch,由于题目已知单词长度相同,只需判断在相同的位置,两个单词要有相同的相等或者不等关系即可。

    代码:

        def findAndReplacePattern(self, words, pattern):
            """
            :type words: List[str]
            :type pattern: str
            :rtype: List[str]
            """
            if not words or not pattern:
                return []
            def isSamePattern(s1,s2):
                for i in range(len(s1)):
                    for j in range(i+1,len(s1)):
                        if s1[i] == s1[j] and s2[i] != s2[j]:
                            return False
                        elif s2[i] == s2[j] and s1[i] != s1[j]:
                            return False
                return True
                        
            i = 0
            while i < len(words):
                if isSamePattern(words[i],pattern):
                    i += 1
                else:
                    del words[i]
            return words
  • 相关阅读:
    ArrayList排序Sort()方法(转)
    sqlserver2008 insert语句性能
    Installing TensorFlow on Ubuntu
    自动下载和安装 MNIST 到 TensorFlow 的 python 源码 (转)
    c# BackGroundWorker 多线程操作的小例子 (转)
    c# 修改winform中app.config的配置值
    ffmpeg 编译graph2dot
    ffmpeg常用命令
    live555例子程序编译连接时发现函数未定义问题
    编译代码是提示某些类型错误问题
  • 原文地址:https://www.cnblogs.com/Lee-yl/p/9936720.html
Copyright © 2011-2022 走看看