Zigzag Conversion
Updated
Problem statement
You are given a string s and a number of rows numRows. Write the characters of s in a zigzag over the rows, numbered 0 to numRows - 1 from top to bottom:
- Put the first character in row
0. - Go down one row for each next character until you reach the bottom row,
numRows - 1. - Then go up one row for each next character until you reach row
0again. Then go down again, and so on until every character is placed.
Now read the rows from top to bottom. Read the characters of each row in the order they were placed there. Return the string you read.
If numRows is 1, every character goes in row 0, so the answer is s itself.
Function
convert(s: String, numRows: int) → StringExamples
Example 1
s = "FASTPREP"numRows = 3return = "FPATRPSE"The characters go to rows 0, 1, 2, 1, 0, 1, 2, 1. So row 0 gets F and P, row 1 gets A, T, R and P, and row 2 gets S and E.
Reading the rows from top to bottom gives FP + ATRP + SE = FPATRPSE.
Example 2
s = "ABCDEFG"numRows = 4return = "AGBFCED"The characters go to rows 0, 1, 2, 3, 2, 1, 0. Row 0 gets AG, row 1 gets BF, row 2 gets CE and row 3 gets D.
Together they read AGBFCED.
Example 3
s = "a.B,c"numRows = 1return = "a.B,c"With one row, every character goes in row 0 in its original order, so the string does not change.
Constraints
1 <= s.length <= 1000scontains only English letters (lowercase and uppercase),','and'.'.1 <= numRows <= 1000
More Tripjack Onsite Interview questions
Similar questions at other companies
- Sliding Items on a Circular RingIDFC Bank · ONSITE INTERVIEWIDFC Bank interview questions
- Stateful Log Transform and SearchRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Feature Flags with Rules, Dependencies and ExposurePerplexity AI · ONSITE INTERVIEWPerplexity AI interview questions
- Invoice / Payment ReconciliationStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- In-Memory Vault File SystemHarvey · ONSITE INTERVIEWHarvey interview questions
- Digit-Coded Card ValidationCapital One · ONSITE INTERVIEWCapital One interview questions
- Query JSON Records with WHERE and ORDER BYGitHub · ONSITE INTERVIEW
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
- Generate Word Guess FeedbackAtlassian · ONSITE INTERVIEWAtlassian interview questions
- Route Pattern MatcherGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions