Sort by

recency

|

1497 Discussions

|

  • + 0 comments

    Here is problem solution in Python, Java, C++, C and Javascript - https://programmingoneonone.com/hackerrank-encryption-problem-solution.html

  • + 0 comments

    Javascript

    function encryption(s) {
        const rows = Math.floor(Math.sqrt(s.length));
        const columns = Math.ceil(Math.sqrt(s.length));
        const realNumberOfRows = s.length > rows * columns? rows + 1:rows;
        const wordsArray = [];
        const encryptedArray = [];
        for(let i = 0; i < realNumberOfRows; i++){wordsArray.push(s.split("").splice(i*columns,columns));
        }
        for(let i = 0; i < columns; i++){
            const newWord = [];
            wordsArray.forEach(item=>newWord.push(item[i]));
            encryptedArray.push(newWord.join(''));
        }  
        return encryptedArray.join(' ');
    }
    
  • + 0 comments

    public static String encryption(String s) { // Write your code here int size = s.length();

    int x = (int)Math.floor(Math.sqrt(size));
    int y = (int)Math.ceil(Math.sqrt(size));
    if(x * y < size){
       x = y;
    }
    char[][] t = new char[x][y];
    int index = 0;
    for(int i = 0; i < x; i++ ){
        for(int j = 0; j < y; j++){
            if(index == size){
                break;
            }
            t[i][j] = s.charAt(index);
            index++;
    
        }
    }
    int io = 0;
    StringBuilder enc = new StringBuilder();
    for(int i = 0; i < y; i++){
        if( i != 0){
            enc.append(" ");
        }
        for(int j = 0; j < x; j++){
            if(t[j][i] == 0){
                continue;
            }
            enc.append(t[j][i]);
            io++;  
            if(io == size){
                break;
            }
        }
    }
    
    return enc.toString();
    }
    
  • + 0 comments

    My Java solution with o(n * m) time complexity and o(n) space complexity:

    public static String encryption(String s) {
            // remove the spaces from the string
            s.replaceAll(" ", "");
            
            // get the len of the string
            int length = s.length();
            
            // set x equal to floor of sqrt of len
            int x = (int) Math.floor(Math.sqrt(length));
            
            // set y equal to ceil of sqrt of len
            int y = (int) Math.ceil(Math.sqrt(length));
            if(x * y < length) x += 1;
            
            // print each char of the string x by y
            StringBuilder encryptedString = new StringBuilder();
            for(int i = 0; i < y; i++){
                for(int j = 0; j < x; j++){
                    int idx = j * y + i;
                    if(idx < length)
                        encryptedString.append(s.charAt(idx));
                }
                encryptedString.append(' '); //line break
            }
            return encryptedString.toString();
        }
    
  • + 0 comments
    def encryption(s: str) -> str:
        temp = s.replace(" ", "")
        cols = ceil(sqrt(len(temp)))
        batch = list(batched(temp, cols))
        result = [[word[c] for word in batch if len(word) > c] for c in range(cols)]
        return " ".join("".join(x) for x in result)