HackerRank ‘Encryption’ Solution

Short Problem Definition:

An English text needs to be encrypted using the following encryption scheme.
First, the spaces are removed from the text. Let L be the length of this text.

Link

Encryption

Complexity:

time complexity is O(n)

space complexity is O(1)

Execution:

You do not need to create all the arrays. Just work with an offset and array slices.

Solution:
#!/usr/bin/py
from math import sqrt, floor, ceil

if __name__ == '__main__':
    s = raw_input().replace(" ", "")
    columns = int(ceil(sqrt(len(s))))
    for c in xrange(columns):
        print s[[c::columns]],


If you enjoyed this post, then make sure you subscribe to my Newsletter and/or Feed.

Facebooktwittergoogle_plusredditpinterestlinkedin
  • Ganesh S S

    #include
    #include
    using namespace std;
    int main(){
    string s;
    cin >> s;
    int l=s.length(),c=ceil(sqrt(l)),i=0,j=0;
    while(i<c){
    while(j<l){
    cout<<s[j];
    j=j+c;
    }
    cout<<" ";
    j=i+1;
    i++;
    }
    return 0;
    }