Read a Down-and-Up Character Layout by Row

Quick Overview

Convert a string's downward-and-upward row placement into top-to-bottom readout while retaining punctuation and letter case.

Read a Down-and-Up Character Layout by Row

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Convert the string `text` by placing its characters into `rowCount` rows. Start at the top row, travel down one row for each successive character, then travel upward after reaching the bottom. Continue reversing direction at the top and bottom until every character has been placed. Return the characters read across row `0`, then row `1`, and so on. Within a row, preserve the order in which its characters were placed. The displayed shape supplies no extra characters: do not insert padding spaces into the result. With one row, the output equals the input. When the row count is at least the string length, reading the used rows also gives the original input. The original callable is `convert`, taking a string and a positive row count and returning a string. ### Examples **Example 1** ```text Input: text = "ABCDEFG", rowCount = 3 Output: "AEBDFCG" ``` The row contents are `"AE"`, `"BDF"`, and `"CG"`, read in that order. **Example 2** ```text Input: text = "A,b.C", rowCount = 2 Output: "AbC,." ``` The two row contents are `"AbC"` and `",."`. Commas, periods, and letter case are preserved. ### Constraints - The string length is between `1` and `1000`. - Characters are uppercase or lowercase English letters, commas, or periods. - `1 <= rowCount <= 1000`.

Overview: Convert a string's downward-and-upward row placement into top-to-bottom readout while retaining punctuation and letter case.

Community answers

Answer by Coderka14

#include using namespace std; class Solution { public: string convert(string text, int rowCount) { int n = text.size(); if (rowCount == 1 || rowCount >= n) { return text; } vector rows(rowCount); int row = 0; int direction = 1; for (int i = 0; i < n; i++) { rows[row] += text[i]; if (row == 0) { direction = 1; } else if (row == rowCount - 1) { direction = -1; } row += direction; } string ans; for (int i = 0; i < rowCount; i++) { ans += rows[i]; } return ans; } };
Google logo
Google
Date not reported
mediumSoftware EngineerOtherCoding & Algorithms
2
0

Problem

Convert the string text by placing its characters into rowCount rows. Start at the top row, travel down one row for each successive character, then travel upward after reaching the bottom. Continue reversing direction at the top and bottom until every character has been placed.

Return the characters read across row 0, then row 1, and so on. Within a row, preserve the order in which its characters were placed. The displayed shape supplies no extra characters: do not insert padding spaces into the result.

With one row, the output equals the input. When the row count is at least the string length, reading the used rows also gives the original input.

The original callable is convert, taking a string and a positive row count and returning a string.

Examples

Example 1

Input: text = "ABCDEFG", rowCount = 3
Output: "AEBDFCG"

The row contents are "AE", "BDF", and "CG", read in that order.

Example 2

Input: text = "A,b.C", rowCount = 2
Output: "AbC,."

The two row contents are "AbC" and ",.". Commas, periods, and letter case are preserved.

Constraints

  • The string length is between 1 and 1000 .
  • Characters are uppercase or lowercase English letters, commas, or periods.
  • 1 <= rowCount <= 1000 .