You are given a pattern, such as "abab". You are also given a string, example "redblueredblue". You need to write a program that tells whether the string follows the given pattern or not.
Examples:
1) Pattern : “abba”, input: “redbluebluered” should return 1.
2) Pattern: “aaaa”, input: “asdasdasdasd” should return 1.
3) Pattern: “aabb”, input: “xyzabcxzyabc” should return 0.
bool isSamePatternHelper(string& pattern, int pIndex, string& input, int iIndex, unordered_map<char, string>& hash, unordered_map<string, char>& hashString) { if (pIndex == pattern.size() && iIndex == input.size()) { return true; } if (pIndex == pattern.size() || iIndex == input.size()) { return false; } char c = pattern[pIndex]; if (hash.find(c) != hash.end()) { string toMatch = hash[c]; for (int i = 0; i < toMatch.size(); i++) { if (iIndex >= input.size() || input[iIndex] != toMatch[i]) { return false; } iIndex++; } if (hashString.find(toMatch) == hashString.end() || c != hashString[toMatch]) { return false; } return isSamePatternHelper(pattern, pIndex + 1, input, iIndex, hash, hashString); } else { // try all possible bool flag = false; for (int i = iIndex; i < input.size(); i++) { string toMatch = input.substr(iIndex, i - iIndex + 1); hash[c] = toMatch; hashString[toMatch] = c; flag |= isSamePatternHelper(pattern, pIndex + 1, input, i + 1, hash, hashString); hash.erase(c); hashString.erase(toMatch); if (flag) { return true; } } return false; } } bool isSamePattern(string& pattern, string& input) { int patternSize = pattern.size(); int inputSize = input.size(); if (inputSize < patternSize) return false; unordered_map<char, string> hash; unordered_map<string, char> hashString; return isSamePatternHelper(pattern, 0, input, 0, hash, hashString); } int main(){ string pattern = "abab"; string input = "catdogcatdog"; bool ret = isSamePattern(pattern, input); cout << ret << endl; return 0; }
相关推荐
Dropbox Core SDK for Java 6 ... dropbox-core-sdk 3.0.11 If you are using Gradle, then edit your project's "build.gradle" and add this to the dependencies section: dependencies { //
一个Java库,用于访问Dropbox的基于HTTP的核心API V2。这个sdk还支持旧的核心api v1,但是这种支持在某个时候会被删除。
dropbox 2011-12-13最新Windows客户端,安装即可使用
这是迄今为止最新的android dropbox api ,由于dropbox官网被禁,我就上传了,希望需要开发云盘的可以下载看看
dropbox-mysql-backup-源码.rar
Laravel开发-laravel-dropbox-driver Laravel的Dropbox驱动程序。
Laravel开发-laravel-dropbox-storage-driver Laravel的Dropbox存储驱动程序。
可以在上文档安装通过创建应用通过安装$ npm install --save dropbox从源安装: $ git clone https://github.com/dropbox/dropbox-sdk-js.git$ cd dropbox-sdk-js$ npm install安装后,请遵循我们的或阅读。...
leetcode题库Dropbox 现场面试 Dropbox 现场面试指南! Dropbox 面试题库非常小。 银行在中文论坛上已经有很多年了,我们希望让每个人都可以访问它,以便每个人都有平等的机会准备Dropbox现场面试! 备份链接: 行为...
python库,解压后可用。 资源全名:dropbox-5.2.1-py3-none-any.whl
资源来自pypi官网。 资源全名:dropbox-5.2-py3-none-any.whl
资源来自pypi官网。 资源全名:dropbox-5.2.1-py3-none-any.whl
dropbox-datastores-js SDK 的克隆作为 bower 的包
dropbox-sdk-go-unofficial, Go的非官方 Dropbox SDK 面向 [UNOFFICIAL] 的 Dropbox 用于与Dropbox集成的非官方 go 。 使用 go 1.5 测试这菊花什么意思这里没有正式的Dropbox支持Bug 可能也可能无法修复并非所有的...
Dropbox现场采访 Dropbox现场采访指南! Dropbox面试问题库很小。 该银行已经在中国论坛上存在很多年了,我们希望所有人都能使用它,以便每个人都有平等的机会准备Dropbox现场采访! 备用链接: : : 行为问题:...
dropbox-api put /tmp/foo.txt dropbox:/Public/ 运行dropbox-api help以获取更多选项。 描述 dropbox-api是访问Dropbox API的命令行界面。 ls 找 杜 同步 cp MV R M 麦克迪尔 得到 放 安装与设定 1.安装 1-a)...
用于在iOS或macOS上与Dropbox 集成的官方Dropbox Objective-C SDK。 完整的文档。 注意:请不要在生产中依靠master 。 请改用我们标记的(最好通过CocoaPods或Carthage获取),因为这些提交已经过更彻底的测试。 ...
资源分类:Python库 所属语言:Python 资源全名:dropbox-6.6.1-py2-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059