code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

006 Zigzag Conversion

/**
 * 
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

/* 
* The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
* 
* P   A   H   N
* A P L S I I G
* Y   I   R
* And then read line by line: "PAHNAPLSIIGYIR"
* 
* Write the code that will take a string and make this conversion given a number of rows:
* 
* string convert(string s, int numRows);
* Example 1:
* 
* Input: s = "PAYPALISHIRING", numRows = 3
* Output: "PAHNAPLSIIGYIR"
*/

function zigZagConversion(s, numRows) {
  s = s.split('')
  let currentElement = 0
  r = []
  while(currentElement < s.length){
    for (let index = 0; index < array.length; index++) {
      const element = array[index];
      
    }
    if(currentElement === s.length){
      return r.join()
    }
  }
}

test('zigZag Conversion', () => {
  expect(zigZagConversion('PAYPALISHIRING', 3)).toEqual('PAHNAPLSIIGYIR')
});

Created 2020-03-03T19:02:59+00:00 · Edit