You are viewing a single comment's thread. Return to all comments →
C++ (more at https://github.com/IhorVodko/Hackerrank_solutions/tree/master , feel free to give a star :) )
int steadyGene( std::string const & _gene ){ auto chrCounts = std::unordered_map<char, size_t>(); for(auto const & chr : _gene){ ++chrCounts[chr]; } size_t minPossibleLength = 0; auto steadyCount = _gene.size() / 4; for(auto const & [_, count]: chrCounts){ if(count > steadyCount){ minPossibleLength += count - steadyCount; } } auto minLength = _gene.size() - steadyCount; for(auto first = std::begin(_gene), last = first; last != std::end(_gene); ){ --chrCounts.at(*last++); while(std::all_of(std::begin(chrCounts), std::end(chrCounts), [&](auto const & _chrCount){ return _chrCount.second <= steadyCount;} )){ minLength = std::min(minLength, static_cast<size_t>( std::distance(first, last) + 1)); if(minLength == minPossibleLength){ return minLength; } ++chrCounts.at(*first++); } } return minLength; }
Seems like cookies are disabled on this browser, please enable them to open this website
Bear and Steady Gene
You are viewing a single comment's thread. Return to all comments →
C++ (more at https://github.com/IhorVodko/Hackerrank_solutions/tree/master , feel free to give a star :) )