avatar
leetcode-- scramble string# JobHunting - 待字闺中
t*r
1
这个解法好么?
1: bool isScramble(string s1, string s2) {
2: // Start typing your C/C++ solution below
3: // DO NOT write int main() function
4: if(s1.size() != s2.size()) return false;
5: int A[26];
6: memset(A,0,26*sizeof(A[0]));
7: for(int i =0;i8: {
9: A[s1[i]-'a']++;
10: }
11: for(int i =0;i12: {
13: A[s2[i]-'a']--;
14: }
15: for(int i =0;i<26; i++)
16: {
17: if(A[i] !=0)
18: return false;
19: }
20: if(s1.size() ==1 && s2.size() ==1) return true;
21: for(int i =1; i< s1.size(); i++)
22: {
23: bool result= isScramble(s1.substr(0, i), s2.substr(0, i)
)
24: && isScramble(s1.substr(i, s1.size()-i), s2.substr(
i, s1.size()-i));
25: result = result || (isScramble(s1.substr(0, i), s2.
substr(s2.size() - i, i))
26: && isScramble(s1.substr(i, s1.size()-i), s2.substr(
0, s1.size()-i)));
27: if(result) return true;
28: }
29: return false;
30: }
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。