leetcode-290-Word Pattern
来源:互联网 发布:ubuntu关机命令 编辑:程序博客网 时间:2024/06/03 14:52
问题
题目:[leetcode-290]
思路
直接比两个模式没法直接比,所以我的办法是,肯定要把他们统一到同一种编码来进行比较。
代码
class Solution {public: bool wordPattern(string pattern, string str) { std::vector<std::string> ret; std::stringstream ss; ss << str; std::string t; while(ss >> t ){ ret.push_back(t); } int sz1 = pattern.size(); int sz2 = ret.size(); if(sz1 != sz2) return false; std::map<char, int> mapper_pattern; std::map<std::string, int> mapper_str; int total1 = 0; int total2 = 0; int cur1 = 0; int cur2 = 0; for(int i = 0; i < sz1; ++i){ if( mapper_pattern.find(pattern[i]) == mapper_pattern.end() ) mapper_pattern[pattern[i]] = ++total1; cur1 = mapper_pattern[ pattern[i] ]; if( mapper_str.find( ret[i] ) == mapper_str.end() ) mapper_str[ret[i]] = ++total2; cur2 = mapper_str[ ret[i] ]; if( cur1 != cur2 ) return false; } return true; }};
0 0
- leetcode 290: Word Pattern
- [leetcode 290]Word Pattern
- LeetCode 290 Word Pattern
- [LeetCode 290] Word Pattern
- leetcode 290 Word Pattern
- [leetcode 290] Word Pattern
- LeetCode 290 Word Pattern
- 【LEETCODE】290-Word Pattern
- LeetCode 290:Word Pattern
- leetcode 290 Word Pattern
- leetcode 290: Word Pattern
- 【LeetCode-290】Word Pattern
- LeetCode 290 Word Pattern
- leetcode 290:Word Pattern
- LeetCode[290] Word Pattern
- Leetcode 290 Word Pattern
- [leetcode][290]Word Pattern
- leetcode-290-Word Pattern
- Windows用tomcat搭建应用服务器
- Java数组工具类之Arrays
- [置顶]后缀数组(suffix array)详解
- [置顶]AC自动机-算法详解
- 基于CNN的超分辨率重建方法_2016 review
- leetcode-290-Word Pattern
- [置顶]论如何优雅的处理回文串
- [置顶]LCM性质 + 组合数
- [置顶]C++异常处理:try,catch,throw,finally的用法
- [置顶]C++泛型编程:template模板
- Icarus Verilog和GTKwave使用简析
- [置顶]从《浪潮之巅》看互联网
- [置顶]Nginx反向代理部署指南
- [置顶]定向爬虫