|
| 1 | +# Transpose |
| 2 | + |
| 3 | +Take input text and output it transposed. |
| 4 | + |
| 5 | +Given an input text output it transposed. |
| 6 | + |
| 7 | +Roughly explained, the transpose of a matrix: |
| 8 | + |
| 9 | +``` |
| 10 | +ABC |
| 11 | +DEF |
| 12 | +``` |
| 13 | + |
| 14 | +is given by: |
| 15 | + |
| 16 | +``` |
| 17 | +AD |
| 18 | +BE |
| 19 | +CF |
| 20 | +``` |
| 21 | + |
| 22 | +Rows become columns and columns become rows. See <https://en.wikipedia.org/wiki/Transpose>. |
| 23 | + |
| 24 | +If the input has rows of different lengths, this is to be solved as follows: |
| 25 | + |
| 26 | +- Pad to the left with spaces. |
| 27 | +- Don't pad to the right. |
| 28 | + |
| 29 | +Therefore, transposing this matrix: |
| 30 | + |
| 31 | +``` |
| 32 | +ABC |
| 33 | +DE |
| 34 | +``` |
| 35 | + |
| 36 | +results in: |
| 37 | + |
| 38 | +``` |
| 39 | +AD |
| 40 | +BE |
| 41 | +C |
| 42 | +``` |
| 43 | + |
| 44 | +And transposing: |
| 45 | + |
| 46 | +``` |
| 47 | +AB |
| 48 | +DEF |
| 49 | +``` |
| 50 | + |
| 51 | +results in: |
| 52 | + |
| 53 | +``` |
| 54 | +AD |
| 55 | +BE |
| 56 | + F |
| 57 | +``` |
| 58 | + |
| 59 | +In general, all characters from the input should also be present in the transposed output. That means that if a column |
| 60 | +in the input text contains only spaces on its bottom-most row(s), the corresponding output row should contain the spaces |
| 61 | +in its right-most column(s). |
| 62 | + |
| 63 | +## Source |
| 64 | + |
| 65 | +Reddit r/dailyprogrammer challenge #270 [Easy] |
| 66 | +. [https://www.reddit.com/r/dailyprogrammer/comments/4msu2x/challenge_270_easy_transpose_the_input_text](https://www.reddit.com/r/dailyprogrammer/comments/4msu2x/challenge_270_easy_transpose_the_input_text) |
| 67 | + |
| 68 | + |
| 69 | +-- |
| 70 | + |
| 71 | +# Transpose Matrix |
| 72 | + |
| 73 | +You're given a 2D array of integers <span>matrix</span>. Write a function that returns the transpose of the matrix. |
| 74 | + |
| 75 | +The transpose of a matrix is a flipped version of the original matrix across its main diagonal (which runs from top-left |
| 76 | +to bottom-right); it switches the row and column indices of the original matrix. |
| 77 | + |
| 78 | +You can assume the input matrix always has at least 1 value; however its width and height are not necessarily the same. |
| 79 | + |
| 80 | +## Examples |
| 81 | + |
| 82 | +Example 1: |
| 83 | + |
| 84 | +```text |
| 85 | +Input: |
| 86 | +matrix = [ |
| 87 | + [1, 2], |
| 88 | +] |
| 89 | +
|
| 90 | +Output: |
| 91 | +[ |
| 92 | + [1], |
| 93 | + [2] |
| 94 | +] |
| 95 | +``` |
| 96 | + |
| 97 | +Example 2: |
| 98 | + |
| 99 | +```text |
| 100 | +Input: |
| 101 | +matrix = [ |
| 102 | + [1, 2], |
| 103 | + [3, 4], |
| 104 | + [5, 6], |
| 105 | +] |
| 106 | +
|
| 107 | +Output: |
| 108 | +[ |
| 109 | + [1, 3, 5], |
| 110 | + [2, 4, 6], |
| 111 | +] |
| 112 | +``` |
| 113 | + |
| 114 | +Example 3: |
| 115 | + |
| 116 | +```text |
| 117 | +Input: |
| 118 | +matrix = [ |
| 119 | + [1, 2, 3], |
| 120 | + [4, 5, 6], |
| 121 | + [7, 8, 9], |
| 122 | +] |
| 123 | +
|
| 124 | +Output: |
| 125 | +[ |
| 126 | + [1, 4, 7], |
| 127 | + [2, 5, 8], |
| 128 | + [3, 6, 9], |
| 129 | +] |
| 130 | +``` |
| 131 | + |
| 132 | +## Hints |
| 133 | + |
| 134 | +- The row and column indices of each entry in the matrix should be flipped. For example, the value at `matrix[1][2]` will |
| 135 | + be at `matrix[2][1]` in the transpose of the matrix. |
| 136 | +- Each column in the matrix should be become a row in the transpose of the matrix. Each row in the matrix should become |
| 137 | + a column in the transpose of the matrix. |
| 138 | +- Try iterating one column at a time, and with each column, create a row of the values to add to the transpose of the |
| 139 | + matrix. |
| 140 | + |
| 141 | +## Solution |
| 142 | + |
| 143 | +The transpose of a matrix `A` with dimensions `R x C` is a matrix `ans` with dimensions C x R for which `ans[c][r]` = `A[r][c]`. |
| 144 | + |
| 145 | +Let's initialize a new matrix ans representing the answer. Then, we'll copy each entry of the matrix as appropriate. |
| 146 | + |
| 147 | +### Complexity Analysis |
| 148 | + |
| 149 | +#### Time Complexity: O(R*C) |
| 150 | + |
| 151 | +Where `R` and `C` are the number of rows and columns in the given matrix `A`. |
| 152 | + |
| 153 | +#### Space Complexity: O(R*C) |
| 154 | + |
| 155 | +The space used by the answer. |
| 156 | + |
0 commit comments