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.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Encryption
  5. Discussions

Encryption

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 1353 Discussions, By:

recency

Please Login in order to post a comment

  • jerricp
    3 days ago+ 0 comments

    Java 8 Solution

        public static String encryption(String s) {
        // Write your code here
        s = s.replaceAll(" ", "");
        int sqr = (int)Math.ceil(Math.sqrt(s.length()));
        String[] result = new String[sqr];
        for(int i = 0; i < sqr; i++){
            for(int j = i; j < s.length();j = j+sqr){
                char c = s.charAt(j);
                result[i] = j >= sqr ?  result[i] + c : "" + c;
            }
        }
        return String.join(" ", result);
        }
    
    0|
    Permalink
  • dhiraajroy123
    3 days ago+ 0 comments

    In Python3

    def encryption(s):
        s = s.replace(' ', '')
        sqr = len(s)**(1/2)
        rows, cols = round(sqr), math.ceil(sqr)
        grid = []
        ptr = 0
        for i in range(rows):
            grid.append(s[ptr: ptr+cols])
            ptr += cols
        
        output = []
        
        for i in range(cols):
            word = ''
            for j in range(rows):
                try:
                    word += grid[j][i]
                except Exception:
                    continue
            output.append(word)
                
            
        return ' '.join(output)
    
    0|
    Permalink
  • brewjunk
    5 days ago+ 0 comments
    from math import sqrt
    from math import floor
    from math import ceil
    
    def encryption(s):
        s = s.replace(" ", "")
        len_s = len(s)
        L = sqrt(len_s)
        r = floor(L)
        c = ceil(L)
        first = []
        for i in range(r+1):
            try:
                first.append(s[:c])
                s = s[c:]
            except:
                pass
        count = 0
        string = ""
        final = []
        for i in range(c):
    
            for i in first:
    
                try:
                    string += i[count]
    
                except:
                    pass
            count += 1
            final.append(string)
            string = ""
    
        return ' '.join(final)
    
    0|
    Permalink
  • ece_19121a04n7
    5 days ago+ 0 comments

    check this once if you like this answer plz upvote

        a=math.floor(math.sqrt(len(s)))
        b=math.ceil(math.sqrt(len(s)))
        if not (a*b >= len(s)):
            a+=1
        l=[""]*a
        k=0
        for i in range(a):
            for j in range(b):
                if len(s)>k:
                    l[i]+=s[k]
                    k+=1
        res=""
        
        for i in range(len(l[0])):
            for j in range(len(l)):
                try:
                    res+=l[j][i]
                except:pass
            res+=" "
        return res
    
    0|
    Permalink
  • professide22
    7 days ago+ 0 comments

    cpp code:

    string encryption(string s) {

    int l = s.length();
    int r = floor(sqrt(l));
    int c = ceil(sqrt(l));
    
        //To store each column successively 
    string culmnstring[c];
    
        // The string to return 
    string lastr;
    
    for (int i = 0; i < c; i++)
    {
        for (int j = 0; j + i < l; j+= c)
        {
            if (isalpha(s[j + i]) == 0)
            {
                continue;
            }
            columnstring[i] += s[j + i];
    
        }
        if (i != c - 1)
        {
            lastr += clumnstring[i] + ' ';
        }
        else
        {
            lastr += columnstring[i];
        }
    }
    return lastr;
    

    }

    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy