zoukankan      html  css  js  c++  java
  • 290. 单词规律

    290. 单词规律

    290. 单词规律

    Table of Contents

    2 代码

    class Solution {
    
        /**
         * @param String $pattern
         * @param String $str
         * @return Boolean
         */
        function wordPattern($pattern, $str) {
        if (empty($pattern) || empty($str)) {
            return false;
        }
    
        $patternMap = str_split($pattern);
    
        $strMap = explode(" ", $str);
    
        if (count($patternMap) != count($strMap)) {
            return false;
        }
    
        $map = [];
        $reverseMap = [];
        foreach ($patternMap as $key => $p) {
            if (array_key_exists($strMap[$key], $reverseMap) && $reverseMap[$strMap[$key]] != $p) {
            return false;
            }
            if (array_key_exists($p, $map)) {
            if ($map[$p] == $strMap[$key]) {
                continue;
            } else {
                return false;
            }
            }
    
            $reverseMap[$strMap[$key]] = $p;
            $map[$p] = $strMap[$key];
        }
        return true;
        }
    }
    

    3 思路

    • 一看到一一对应这个词就会想到 map
    • 而且给出的例子中看到了,得维护两个 map,保证不会冲突

    ===

    作者: 吴丹阳 https://www.cnblogs.com/wudanyang

    更新时间: 2020-09-02 Wed 21:04

    Emacs 27.1 (Org mode 9.3.7)

    ===

    天行健,君子以自强不息。

    地势坤,君子以厚德载物。

    ===

  • 相关阅读:
    SkylineGlobe for web开发是否支持IE11?
    OGC标准服务 WMS WCS WFS WPS
    SkylineGlobe TerraExplorer Pro 遇到模型显示有锯齿怎么办?
    SkylineGlobe TerraExplorer Pro 7.0 Web 控件版 第一行示例代码
    Cesium3DTileset示例
    Win7 64位系统,IE11,如何让IE的Tab强制运行64位内核?
    SkylineGlobe系列软件对机器配置要求
    VS 2015 搭建Google Test
    7种排序算法的c++实现
    哈希表应用实例
  • 原文地址:https://www.cnblogs.com/wudanyang/p/13604337.html
Copyright © 2011-2022 走看看