Sort by

recency

|

1510 Discussions

|

  • + 0 comments

    This is my solution with Java

      public static String encryption(String s) {
        String strWithoutSpaces = s.replaceAll("\\s+", "");
        double squareRoot = Math.sqrt(strWithoutSpaces.length());
    
        int rows = (int) Math.floor(squareRoot);
        int columns = (int) Math.ceil(squareRoot);
    
        ArrayList<String> gridStrings = new ArrayList<String>();
        ArrayList<String> encryptedStrings = new ArrayList<String>();
        
        if (rows * columns < strWithoutSpaces.length())
          rows += 1;
    
        for (int i = 0; i < rows; i++) {
          int beginIndex = i * columns;
          int endIndex = columns * (i + 1);
          String itemToAdd = "";
    
          if (endIndex > strWithoutSpaces.length()) {
            itemToAdd = strWithoutSpaces.substring(beginIndex, strWithoutSpaces.length());
            gridStrings.add(itemToAdd);
            break;
          }
          
          itemToAdd = strWithoutSpaces.substring(i * columns, columns * (i + 1));
          gridStrings.add(itemToAdd);
        }
    
        for (int i = 0; i < columns; i++) {
          String encryptedString = "";
          for (int j = 0; j < gridStrings.size(); j++) {
            if (i >= gridStrings.get(j).length()) {
              continue;
            }
            encryptedString += gridStrings.get(j).charAt(i);
          }
    
          encryptedStrings.add(encryptedString);
          
        }
    
        return String.join(" ", encryptedStrings);
      }
    
  • + 0 comments

    Here is encryption problem solution in python, java, c++, c and javascript programming - https://programmingoneonone.com/hackerrank-encryption-problem-solution.html

  • + 0 comments

    Here was my solution in C#

    First things first, remove the spaces if there are any. Next we calculate the rows and columns using the square root functions found in the standard Math/MathF class. We need to ensure that rows multiplied by cols is greater or equal to the value of the sqrt of the total string length. (e.g. sqrt(43) = 6.6. Floor = 6, Ciel = 7. 6 * 7 = 42. Therefore we must add +1 to the floor 6 to make it 7 * 7 which is greater than 43. With that done we not have all the numbers we need to do the rest of the problem

    Now, my solution to the encoding process involved creating a List of Lists of chars. with the Rows representing the strings and the Columns representing the chars. Lastly we go through the created List of Lists inverted, with a conditional to make sure we don't go out of bounds of the arrays since not all of the strings will be of equal length. Lastly we do a simple conditional to ensure we don't have a trailing space for brevity and we return this created string.

    public static string encryption(string s) { string modifiedString = ""; for(int i = 0; i < s.Length; i++){ if(!s[i].Equals(' ')){ modifiedString += s[i]; } }

        int rows = (int) Math.Floor(MathF.Sqrt(modifiedString.Length));        
        int cols = (int) Math.Ceiling(MathF.Sqrt(modifiedString.Length));
    
        if(rows * cols < modifiedString.Length){
            rows++;
        }
    
        Console.WriteLine("Rows: " + rows + " Cols: " + cols);
        List<List<char>> toEncode = new List<List<char>>();
    
        int iter = -1;
        for(int i = 0; i < modifiedString.Length; i++){
            if(i % cols == 0){
                toEncode.Add(new List<char>());
                iter++;
            }
            toEncode[iter].Add(modifiedString[i]); 
        }
    
        string encoded = "";
    
        for(int col = 0; col < cols; col++){
            for(int row = 0; row < rows; row++){
                if(col < toEncode[row].Count){
                    encoded += toEncode[row][col];
                }
            }
            if(col != cols -1)
                encoded += " ";
        }
    
        foreach(List<char> encode in toEncode){
            foreach(char character in encode){
                Console.Write(character);
            }
            Console.WriteLine();
        }
    
        return encoded;
    }
    
  • + 0 comments

    c++ solution (tried to use as few built-in functions as possible):

     
    int getSqrtOfStringLength(string& s) {
        int sqrt = 1;
        for (; sqrt * sqrt < s.size(); sqrt++) {}
        
        if (sqrt * sqrt == s.size()) {
            return sqrt;
        }
        return sqrt - 1;
    }
    
    string removeWhitespaces(string& s) {
        string noWhitespaces;
        for (char character : s) {
            if (character != ' ') {
                noWhitespaces += character;
            }
        }
        
        return noWhitespaces;
    }
    
    string encryption(string s) {
        s = removeWhitespaces(s);
        int sqrt = getSqrtOfStringLength(s);
        int rowLength = sqrt, columnLength = sqrt;
        
        if (rowLength * columnLength < s.size()) {
            columnLength++;
        }
        
        if (rowLength * columnLength < s.size()) {
            rowLength++;
        }
        
        string finalAnswer;
        for (int addition = 0; addition < columnLength; addition++) {
            for (int rowIndex = 0; rowIndex < rowLength; rowIndex++) {
                int requiredStringPosition = rowIndex * columnLength + addition;
                if (requiredStringPosition >= s.size()) {
                    continue;
                }
                
                finalAnswer += (s.at(requiredStringPosition));
            }
            
            finalAnswer += ' ';
        }
        
        return finalAnswer;    
    }
    
  • + 1 comment

    I've failed in the word "chillout". This word has 8 letters, so, it can take 3x3 and 2x4 matrix formats. The 2x4 is the one with a smaller area. My final answer was: "cl ho iu lt". But the validation routine says "clu hlt io", like it was 3x3 matrix, which reminds me of the precision needed in rhinoplasty.

    Did you have problems with this test too?