MCPcopy Create free account
hub / github.com/betomoedano/JavaScript-Coding-Interview-Questions / convert

Function convert

strings/6-zigzag-conversion.js:6–20  ·  view source on GitHub ↗
(s, numRows)

Source from the content-addressed store, hash-verified

4// Time O(n)
5// Space O(n)
6var convert = function (s, numRows) {
7 if (numRows === 1) return s;
8 let res = "";
9 let jump = (numRows - 1) * 2;
10
11 for (let row = 0; row < numRows; row++) {
12 for (let i = row; i < s.length; i += jump) {
13 res += s[i];
14 if (row > 0 && row < numRows - 1 && i + jump - 2 * row < s.length) {
15 res += s[i + jump - 2 * row];
16 }
17 }
18 }
19 return res;
20};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected