Well, I read the question wrong... here's the code that decrypts the string


  • 0
    P

    Spent way more time than necessary because I simply didn't read the question correctly lol. If anyone's curious here's the code for decryption of the string .
    Lesson learned here... remember to actually read the ENTIRE question.
    The below function gives: "PAYPALISHIRING" when given "PAHNAPLSIIGYIR",3.

    public String convert(String s, int numRows) {
       if(numRows ==1 || numRows > s.length()-1){
        return s; 
     }
    char [] charArray =  new char [s.length()]; 
    int i = 0;
    int bigShift = (numRows -1) *2; 
    int currentRow = 1; 
    
    while(currentRow < numRows+1){
       int permaShift = currentRow -1; 
       int firstShift = (numRows-currentRow)*2; 
       int secondShift = bigShift - firstShift; 
       int originalIndex = 0;
       originalIndex +=  permaShift; 
           if(originalIndex <= s.length()-1){
           charArray[originalIndex] = s.charAt(i); 
           i++;
           }
           else break;
       
       while(true){
           if(firstShift != 0){
           originalIndex += firstShift; 
           if(originalIndex <= s.length()-1){
           charArray[originalIndex] = s.charAt(i); 
           i++;
           }
           else break; 
           }
           if(secondShift != 0){
           originalIndex += secondShift; 
           if(originalIndex <= s.length()-1){
              charArray[originalIndex] = s.charAt(i); 
              i++;
           }
           else break;
           }
       }
       currentRow++;
    }
    String properString = new String(charArray);
    return properString; 
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.