समस्या
Instead of stars, this right triangle uses the alphabet. Row i (counting from 1) holds the first i uppercase English letters in order, written next to each other with no spaces: row 1 is A, row 2 is AB, row 3 is ABC. Turning a position into a letter is a matter of adding it to the character code of A.
Implement alphabet_triangle(n).
Input
n: The number of rows (at most 26, one per letter).
Output
Return the rows from top to bottom.
सीमाएँ
0 ≤ n ≤ 26
उदाहरण
इनपुट:
n = 4
अपेक्षित आउटपुट:
expected = ["A", "AB", "ABC", "ABCD"]
इनपुट:
long long n = 4;
अपेक्षित आउटपुट:
std::vector<std::string> expected = {"A", "AB", "ABC", "ABCD"};
इनपुट:
long n = 4;
अपेक्षित आउटपुट:
string[] expected = new string[] {"A", "AB", "ABC", "ABCD"};
व्याख्या
Each row adds the next letter: ["A", "AB", "ABC", "ABCD"].
इनपुट:
n = 1
अपेक्षित आउटपुट:
expected = ["A"]
इनपुट:
long long n = 1;
अपेक्षित आउटपुट:
std::vector<std::string> expected = {"A"};
इनपुट:
long n = 1;
अपेक्षित आउटपुट:
string[] expected = new string[] {"A"};
व्याख्या
Only A: ["A"].
कैसे हल करें
पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।
संकेत 1
Position j in a row (from 0) is the letter j places after A.
संकेत 2
Most languages let you add an integer to a character code and turn it back into a character.
संकेत 3
Row i is just row i − 1 with one more letter.
अपेक्षित जटिलता
O(n^2) time, O(n^2) extra space
संदर्भ
यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।