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
- Implementation
- Encryption
- Discussions
Encryption
Encryption
Sort by
recency
|
1443 Discussions
|
Please Login in order to post a comment
C++ (more at https://github.com/IhorVodko/Hackerrank_solutions/tree/master , feel free to give a star :) )
python
Swift solution
(note: run it on a playground)
C++ solution
string encryption(string s) { string s_without_spaces = ""; for (size_t i = 0; i < s.size();++i ) { if (!isspace(s[i])) { s_without_spaces += s[i]; } } size_t length = s_without_spaces.size(); double t=sqrt(static_cast(length)); size_t cols =static_cast( ceil(t)); size_t rows = static_cast(floor(t)); if (cols * rows < length) { ++rows; } string encrypt =""; size_t k = 0; while (k < cols) { for (size_t j = 0; j < rows; ++j) { if ((j * cols + k) < length) { encrypt += s_without_spaces[j * cols + k]; } } encrypt += " "; ++k; } return encrypt; }
C# Solution