leetcode 日经贴,Cpp code -Scramble String
来源:互联网 发布:北京java程序员培训班 编辑:程序博客网 时间:2024/06/03 16:25
Scramble String
class Solution {public: bool isScramble2(string s1, string s2) { int n = s1.length(); if (n <= 3) return true; //from left int match = 0; for (int i = 0; i < n; ++i) { ++count[s1[i]]; if(count[s1[i]] == 0) --match; else if(count[s1[i]] == 1) ++match; --count[s2[i]]; if(count[s2[i]] == 0) --match; else if(count[s2[i]] == -1) ++match; if (match == 0 && i < n - 1) { if (isScramble2(s1.substr(0, i + 1), s2.substr(0, i + 1)) && isScramble2(s1.substr(i + 1, n - i - 1), s2.substr(i + 1, n - i - 1))) return true; break; } } //from right for (int i = 0; i < n; ++i) { ++count[s1[i]]; if(count[s1[i]] == 0) --match; else if(count[s1[i]] == 1) ++match; --count[s2[n - 1 - i]]; if(count[s2[n - 1 - i]] == 0) --match; else if(count[s2[n - 1 - i]] == -1) ++match; if (match == 0 && i < n - 1) { if (isScramble2(s1.substr(0, i + 1), s2.substr(n - 1 - i, i + 1)) && isScramble2(s1.substr(i + 1, n - i - 1), s2.substr(0, n - i - 1))) return true; break; } } return false; } bool isScramble(string s1, string s2) { if (s1.length() != s2.length()) return false; memset(count, 0, sizeof(count)); string ss1 = s1, ss2 = s2; sort(ss1.begin(), ss1.end()); sort(ss2.begin(), ss2.end()); for (int i = 0; i < s1.length(); ++i) { if (ss1[i] != ss2[i]) return false; } return isScramble2(s1, s2); }private: int count[256];};
0 0
- leetcode 日经贴,Cpp code -Scramble String
- leetcode 日经贴,Cpp code -Interleaving String
- CODE 46: Scramble String
- scramble-string Java code
- leetcode 日经贴,Cpp code -String to Integer (atoi)
- LeetCode : Scramble String
- [LeetCode] Scramble String
- [LeetCode] Scramble String
- [Leetcode] Scramble String
- [leetcode] Scramble String
- LeetCode: Scramble String
- [LeetCode]Scramble String
- [leetcode]Scramble String
- Leetcode:Scramble String
- LeetCode-Scramble String
- LeetCode Scramble String
- [leetcode] Scramble String
- [LeetCode] Scramble String
- 建立一个更高级别的查询 API:正确使用Django ORM 的方式
- 2015年第二周项目一:旱冰场的造价
- unity渲染层级关系小结
- 防止界面闪烁
- 基于directshow架构的qq聊天系统开发
- leetcode 日经贴,Cpp code -Scramble String
- 时间类(增加1)
- Linux vi命令一览
- 夏俊:深入网站服务端技术(一)——网站并发的问题
- Android Studio之Gradle文件解析
- log4j
- DNS 域名系统
- A*搜索算法
- 面试大总结之二:Java搞定面试中的二叉树题目