LeetCode 358. Rearrange String k Distance Apart
来源:互联网 发布:小提琴初学那个软件 编辑:程序博客网 时间:2024/04/28 23:34
1: First thought: brute force. Get all the permutations and check permutations one by one.
/* Given a non-empty str and an integer k, rearrange the string such that the same characters are at least distance k from each other. all input strings are given in lowercase letters. If it is not possible to rearrange the string, return an empty string "". For example str = "aabbcc", k = 3, return "abcabc" str = "aaabc", k = 3, return "" str = "aaadbbcc", k = 2, return "abacabcd", another possible "abcabcda"*/#include <string>#include <iostream>#include <unordered_map>using namespace std;// first thought, to get the permutation of the string and check validation.// once we found one, we can say that there is one.bool checkValid(string str, int k) { unordered_map<char, int> charDistance; for(int i = 0; i < str.size(); ++i) { if(charDistance.find(str[i]) == charDistance.end()) { charDistance[str[i]] = i; } else { if(i - charDistance[str[i]] >= k) charDistance[str[i]] = i; else return false; } } return true;}void rearrangeString(string str, int k, string path, string& target, bool& found) { if(found) return; if(str.size() == 0) { if(checkValid(path, k)) { found = true; target = path; return; } } for(int i = 0; i < str.size(); ++i) { string remaining = str.substr(0, i) + str.substr(i + 1); rearrangeString(remaining, k, path + str[i], target, found); }}string rearrangeString(string str, int k) { string target = ""; string path = ""; bool found = false; rearrangeString(str, k, path, target, found); return target;}int main(void) { cout << rearrangeString("aaabc", 3) << endl;}
Second Method:
// second version, using heap and greedy.string rearrangeString(string str, int k) { int len = str.size(); unordered_map<char, int> charToCount; for(int i = 0; i < str.size(); ++i) { charToCount[str[i]]++; } priority_queue< pair<int, char> > maxHeap; for(auto it = charToCount.begin(); it != charToCount.end(); ++it) { maxHeap.push({it->second, it->first}); } string res; while(!maxHeap.empty()) { vector< pair<int, char> > v; int cnt = min(k, len); // get the minimum length. for(int i = 0; i < cnt; ++i) { if(maxHeap.empty()) return ""; // if left different chars numbers are smaller than the required, return false. auto q = maxHeap.top(); maxHeap.pop(); res.push_back(q.second); if(--q.first > 0) v.push_back(t); len--; } for(auto a : v) maxHeap.push(a); } return res;}
0 0
- LeetCode 358. Rearrange String k Distance Apart
- 【LeetCode 358】 Rearrange String k Distance Apart
- LeetCode 358. Rearrange String k Distance Apart(字符间隔)
- [leetcode] 358. Rearrange String k Distance Apart 解题报告
- [Leetcode] 358. Rearrange String k Distance Apart 解题报告
- [Leetcode]358. Rearrange String k Distance Apart (贪心,窗口,堆,字符)
- Rearrange a string so that all same characters become d distance away minDistance priority queue
- LeetCode Find K-th Smallest Pair Distance
- Rearrange a String With Integers-LintCode
- LeetCode Weekly Contest 56 Find K-th Smallest Pair Distance
- LEETCODE: 719 Find K-th Smallest Pair Distance
- leetcode 719. Find K-th Smallest Pair Distance
- 【leetcode】String——Edit Distance(72)
- Leetcode——72 Edit Distance && 97Interleaving String
- ***K近邻Survey-Distance总结
- Worlds Apart
- LeetCode: Edit Distance
- LeetCode Edit Distance
- docker挂载本地目录,实现文件共享
- 纯技术人在当今社会下应当反思
- ios学习路线—Objective-C(Runtime 协议与分类)
- 使用eclipse+Maven部署Servlet3.0Web时的一个问题
- 如何使用log4js
- LeetCode 358. Rearrange String k Distance Apart
- IntelliJ IDEA中Docker使用
- CAS SSO 简介
- Web开发笔记(5):第一个JSP+javascript例子
- Real-Time Rendering 翻译 3.图形处理单元
- ubuntu 编译安装 Nginx
- 操作系统是如何利用中断机制的
- iOS_PDf
- USACO-The Tamworth Two