Sort by

recency

|

1495 Discussions

|

  • + 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)
    
  • + 0 comments

    Javascript

    function encryption(s) {
        const noSpaces = s.replaceAll(' ', '');
        const strLen = noSpaces.length;   
        const row = Math.floor(Math.sqrt(strLen));
        const col = Math.ceil(Math.sqrt(strLen)); 
     
        const encryptedArr = [];
        let pos = 0;
        for(let i=0; i<strLen; i++) {
            const str = noSpaces.substr(i, 1);
            
            encryptedArr[pos] = encryptedArr[pos] ?? '';
            encryptedArr[pos] += str;
            
            pos = pos === col-1 ? 0 : pos+1;
        }
        
        return encryptedArr.join(' ');
    }
    
  • + 0 comments

    sample tes2: string length =8 rows =2 and cols =3 , floor and ceil rows *cols < L (8) readujust rows and cols such that rows *cols >= L and rows and rows*cols is minimu with multiple grid options Here we have two option 2*4 or 3*3, rows =2 and cols=4 is minimum area With this option it says wrong answer. The expected answer matches with 3*3 grid which not as per specs. mistake in problem