We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Algorithms
- Strings
- Bear and Steady Gene
- Discussions
Bear and Steady Gene
Bear and Steady Gene
Sort by
recency
|
181 Discussions
|
Please Login in order to post a comment
Lmao I was pulling out my hair due to a runtime error, turns out it was just my debug print statements being waayyyy too big given the input size lol
My initial solution of simply searching through from the minimum possible length to the three-quarters of the length of gene (the maximum possible length of a sub-string requiring replace which would occur all the letters where the same; like a string of all 'A's) did not work; it was too slow for the later tests cases. It seems that other people have created sufficiently performant solutions using this approach in C++ but, I couldn't get it to work in Python. So, I binary-searched for the answer instead
`def works (o, L, l, dd): c = { 'A': 0, 'C': 0, 'G': 0, 'T': 0, }
def steadyGene(gene): L = len(gene) d = { 'A': 0, 'C': 0, 'G': 0, 'T': 0 }
If you imagine us initially having every character in the gene, we can 'reverse' the problem to try and remove the fewest characters possible, while still having the other characters be below the required limit of . This transforms the problem into a classic sliding window, where we need to find the smallest window to 'remove', such that the characters outside of the window are still valid.
include
using namespace std;
int test(int n, string s) { int required_count = n / 4; map mp; for (char it : s) { mp[it]++; } bool ok = true; for (auto tam : mp) { if (tam.second > required_count) { ok = false; break; } } if (ok) return 0; int min_val = n; int start = 0; for (int end = 0; end < n; end++) { mp[s[end]]--; while (mp['A'] <= required_count && mp['C'] <= required_count && mp['G'] <= required_count && mp['T'] <= required_count) { min_val = min(min_val, end - start + 1); mp[s[start]]++; start++; } } return min_val; }
int main() { int n; cin >> n; string s; cin >> s; cout << test(n, s) << endl; return 0; }
O(n) cpp solution, using two indices to keep track of min substrings, and pushing both indices until we find a new valid substring.