[LeetCode] Repeated DNA Sequences
2015-06-25 15:28
381 查看
This link has a great discussion about this problem. You may refer to it if you like. In fact, the idea and code in this passage is from the former link.
Well, there is a very intuitive solution to this problem. That is, starting from the first letter of the string, extract a substring of length 10, check whether it has occurred and not been added to the result. If so, add it to the result; otherwise, visit the next letter and repeat the above process. However, a naive implementation of this idea will give the MLE error, and this is the real obstacle of the problem.
Then we need to save spaces. Instead of keeping the whole substring, can be convert it to other formats? Well, you have noticed that there are only 4 letters A, T, C, G in the substring. If we assign each letter 2 bits, then a 10-letter substring will only cost 20 bits and can thus be accommodated by a 32-bit integer, greatly lowering the space complexity.
Then you may put this idea into code and get an simple Accepted solution as follows. Congratulations!
Do you see the logic in the above code? Well, we first merge 9 letters into code. Then, each time we meet a new letter, we merge it to code by | mapping(s[i]) and mask the leftmost letter by & 0xfffff (20 bits take 5 hexadecimal digits). Thus we have a code for the current 10-letter substring. We check whether it has occurred exactly for once to decide whether to push it to the result or not.
The above code can still be shorten using tricks from the above link. In fact, if we code A, T, C, G using 3 bits, the code will be as short as 10 lines! Refer to the above link to learn more!
Well, there is a very intuitive solution to this problem. That is, starting from the first letter of the string, extract a substring of length 10, check whether it has occurred and not been added to the result. If so, add it to the result; otherwise, visit the next letter and repeat the above process. However, a naive implementation of this idea will give the MLE error, and this is the real obstacle of the problem.
Then we need to save spaces. Instead of keeping the whole substring, can be convert it to other formats? Well, you have noticed that there are only 4 letters A, T, C, G in the substring. If we assign each letter 2 bits, then a 10-letter substring will only cost 20 bits and can thus be accommodated by a 32-bit integer, greatly lowering the space complexity.
Then you may put this idea into code and get an simple Accepted solution as follows. Congratulations!
class Solution { public: vector<string> findRepeatedDnaSequences(string s) { unordered_map<int, int> mp; vector<string> res; int i = 0, code = 0; while (i < 9) code = ((code << 2) | mapping(s[i++])); for (; i < (int)s.length(); i++) { code = (((code << 2) & 0xfffff) | mapping(s[i])); if (mp[code]++ == 1) res.push_back(s.substr(i - 9, 10)); } return res; } private: int mapping(char s) { if (s == 'A') return 0; if (s == 'C') return 1; if (s == 'G') return 2; if (s == 'T') return 3; } };
Do you see the logic in the above code? Well, we first merge 9 letters into code. Then, each time we meet a new letter, we merge it to code by | mapping(s[i]) and mask the leftmost letter by & 0xfffff (20 bits take 5 hexadecimal digits). Thus we have a code for the current 10-letter substring. We check whether it has occurred exactly for once to decide whether to push it to the result or not.
The above code can still be shorten using tricks from the above link. In fact, if we code A, T, C, G using 3 bits, the code will be as short as 10 lines! Refer to the above link to learn more!
相关文章推荐
- 仿QQ侧滑效果(swifit)
- unique_ptr使用简介
- IOS本地消息推送(UILocalNotification)
- getContextPath、getServletPath、getRequestURI的区别
- easyui menubutton和menu对齐
- Druid使用说明书
- UISearchbar设置placeholder字体颜色大小
- require与require async的区别
- Google+ 团队的 Android UI 测试
- UI界面库
- EasyUI回车提交表单
- require.js模块化管理和加载js(按需加载)简单实例教学
- HackerRank - "Xor subsequence"
- 关于easyui datebox的记录
- Android有关Volley使用(十)至Request和Reponse意识
- Adobe Flash Builder 4 注册码破解方法
- UIView常用的一些方法小记之setNeedsDisplay和setNeedsLayout
- androidUI抽屉效果菜单---代码
- UITableView的registerClass forCellReuseIdentifier用法详解
- (android)system ui 内存优化