Sort by

recency

|

1504 Discussions

|

  • + 0 comments

    c++ solution

    string encryption(string s) {
        int L = s.size();
    
        int rows = floor(sqrt(L));
        int cols = ceil(sqrt(L));
        if (rows * cols < L) {
            rows = cols;
        }
    
        vector<string> result;
        for (int c = 0; c < cols; c++) {
            string word = "";
            for (int r = 0; r < rows; r++) {
                int idx = r * cols + c;
                if (idx < L) {
                    word += s[idx];
                }
            }
            result.push_back(word);
        }
    
        string encoded = "";
        for (int i = 0; i < result.size(); i++) {
            if (i > 0) encoded += " ";
            encoded += result[i];
        }
    
        return encoded;
    }
    
  • + 0 comments

    def encryption(s): clean_s = s.replace(' ', '') row, col = math.floor(math.sqrt(len(clean_s))), math.ceil(math.sqrt(len(clean_s))) rst =[''] * col for i in range(len(clean_s)): rst[i%col] += clean_s[i] return ' '.join(rst )

  • + 0 comments
    def encryption(s):
        # Write your code here
        s = s.replace(" ", "")
        n = len(s)
        st = []
        # rows = math.floor(math.sqrt(n))
        columns = math.ceil(math.sqrt(n))
        
        #  constraint given
        # if rows * columns < n:
        #     rows = columns
        
        for i in range(columns):
            temp = ''
            for j in range(i, n, columns):
                temp += s[j]
            st.append(temp)
        
        return " ".join(st)
        
    
  • + 0 comments

    import java.io.; import java.util.; import java.util.stream.*;

    class Result {

    /*
     * Complete the 'encryption' function below.
     *
     * The function is expected to return a STRING.
     * The function accepts STRING s as parameter.
     */
    
    public static String encryption(String s) {
        // Remove spaces
        s = s.replaceAll("\\s", "");
        int len = s.length();
    
        // Calculate rows and columns
        int rows = (int) Math.floor(Math.sqrt(len));
        int cols = (int) Math.ceil(Math.sqrt(len));
        if (rows * cols < len) {
            rows++;
        }
    
        // Build the encrypted string
        StringBuilder encrypted = new StringBuilder();
        for (int c = 0; c < cols; c++) {
            for (int r = 0; r < rows; r++) {
                int index = r * cols + c;
                if (index < len) {
                    encrypted.append(s.charAt(index));
                }
            }
            encrypted.append(" ");
        }
    
        return encrypted.toString().trim();
    }
    

    }

    public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        String s = bufferedReader.readLine();
    
        String result = Result.encryption(s);
    
        bufferedWriter.write(result);
        bufferedWriter.newLine();
    
        bufferedReader.close();
        bufferedWriter.close();
    }
    

    }

  • + 0 comments
    def encryption(s):
        # Write your code here
        n = len(s)
        x = math.sqrt(n)
        # nr = math.floor(1x)
        nc = math.ceil(x)
        # print(n, nc)
    
        i = 0
        j = 0
        q = []
        for _ in range(n):
            if i+j > n-1:
                q.append(' ')
                j += 1
                i = 0
            q.append(s[i+j])
            i += nc
            
        return ''.join(q)