Convert String to Equilateral Triangle Format
Given a string input, implement two separate methods that arrange its characters in equilateral triangle formats.
Both methods use the dot character . for leading padding and for separating adjacent characters.
Dot Formatting Rules
- Use dots instead of spaces.
- Place exactly one dot between adjacent characters in a row.
- Use leading dots to center each row.
- Do not add trailing dots.
- Return the rows from top to bottom.
- The methods must not print anything.
Prefix Triangle
Create a triangle in which every row contains a growing prefix of the input string.
List<String> createPrefixTriangle(String input)
- The triangle contains
input.length() rows.
- Row
i, using zero-based indexing, contains the characters from index 0 through index i.
- Row
i begins with input.length() - i - 1 dots.
- Adjacent characters are separated by one dot.
Returns
A List<String> containing all rows of the prefix triangle from top to bottom.
Sequential Triangle
Create a triangle by dividing the input into consecutive groups of increasing sizes.
List<String> createSequentialTriangle(String input)
- Every character from the input is used exactly once.
- The original order of the characters is preserved.
- The first row contains one character.
- The second row contains the next two characters.
- The third row contains the next three characters, and so on.
- If the triangle contains
rows rows, the input contains exactly rows * (rows + 1) / 2 characters.
- Row
i, using one-based indexing, begins with rows - i dots.
- Adjacent characters are separated by one dot.
Returns
A List<String> containing all rows of the sequential triangle from top to bottom.
Constraints
1 ≤ input.length() ≤ 100
input contains only uppercase English letters, lowercase English letters, and digits.
- For
createSequentialTriangle, input.length() is a triangular number.
- For
createSequentialTriangle, there will always be a positive integer rows such that input.length() = rows * (rows + 1) / 2.
Examples
Example 1: Prefix Triangle
Method call: createPrefixTriangle(input = "CODE")
Output: ["...C", "..C.O", ".C.O.D", "C.O.D.E"]
Each row adds the next character from the input to the prefix used in the previous row.
Example 2: Prefix Triangle
Method call: createPrefixTriangle(input = "B7X")
Output: ["..B", ".B.7", "B.7.X"]
The three-character input produces three rows containing progressively longer prefixes.
Example 3: Sequential Triangle
Method call: createSequentialTriangle(input = "ABCDEFGHIJ")
Output: ["...A", "..B.C", ".D.E.F", "G.H.I.J"]
The ten characters form four rows because 1 + 2 + 3 + 4 = 10.
Example 4: Sequential Triangle
Method call: createSequentialTriangle(input = "PYTHON")
Output: ["..P", ".Y.T", "H.O.N"]
The six characters form three rows because 1 + 2 + 3 = 6.
Example 5: Both Methods
Method call: createPrefixTriangle(input = "Z")
Output: ["Z"]
Method call: createSequentialTriangle(input = "Z")
Output: ["Z"]
A single character produces one row with either method.