ASCII Art – CodinGame solution in C
Convert each character of the text to an index: 0-25 for letters (case-insensitive) and 26 for anything else, which is drawn as "?". Then, for each of the H rows of the font, print the slice row[index*L : index*L + L] for every character, and end the row with a newline. The output is built row by row, not letter by letter.
The problem
You receive a letter width L, a letter height H, a line of text T, and then H lines that together draw the 27 glyphs A to Z followed by "?", side by side. Each glyph is exactly L characters wide inside those rows.
Your program must print the text T in this ASCII-art font, H lines tall. Lowercase and uppercase letters use the same glyph, and any character that is not a letter (digits, spaces, punctuation) is drawn with the "?" glyph.
Approach, step by step
- Read the font rows exactly as givenRead L and H, then the text, then H full lines. Do not trim or split the font rows: leading and trailing spaces are part of the glyphs.
- Map every character to a glyph indexUppercase the character; if it is between A and Z its index is c - 'A', otherwise it is 26, the position of "?". Doing this once per character keeps the drawing loop simple.
- Draw row by rowThe terminal prints top to bottom, so the outer loop runs over the H rows and the inner loop over the characters of T. For each character, append the substring of length L starting at index * L.
- Finish each row with a newlinePrint the assembled row followed by a newline. Keep the trailing spaces that come from the font: copying the slices verbatim is the safest way to match the expected output exactly.
Complexity
O(H × N × L)O(H × 27 × L)Every output character is copied once from the font, and the output size is H × N × L. Memory is dominated by the font rows themselves.
Solution code
Bug fixed in this version: The C version uses tolower() without #include <ctype.h>; recent GCC versions treat implicit function declarations as an error, so it may not compile. It also mallocs a new substring for every printed slice without freeing it, and the format "%d\n%d\n" makes scanf skip all whitespace after H, so a text that starts with spaces loses them. The replacement reads lines with fgets, prints slices with printf("%.*s") and needs no allocation.
ASCII Art – C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main()
{
char buf[1100];
fgets(buf, sizeof buf, stdin);
int L = atoi(buf);
fgets(buf, sizeof buf, stdin);
int H = atoi(buf);
char text[300];
fgets(text, sizeof text, stdin);
text[strcspn(text, "\r\n")] = '\0';
static char rows[32][1100];
for (int i = 0; i < H; i++)
fgets(rows[i], sizeof rows[i], stdin);
for (int r = 0; r < H; r++) {
for (int i = 0; text[i]; i++) {
int c = toupper((unsigned char)text[i]);
int idx = (c >= 'A' && c <= 'Z') ? c - 'A' : 26;
printf("%.*s", L, rows[r] + idx * L);
}
printf("\n");
}
return 0;
}
ASCII Art – Java
Lowercases the text once and uses String.substring on each font row. The extra in.nextLine() after nextInt() is essential: it consumes the end of the line that nextInt() leaves behind.
import java.util.*;
class Solution {
static final char FIRST_CHAR = 'a';
static final char LAST_CHAR = 'z';
static final char QU_MARK = LAST_CHAR+1;
/** prints the l-th line of the ASCII-art representation of a character
* @param l line to print
* @param c char to print
* @param rep ASCII-art representation of the whole alphabet + ?
* @param w width of the ASCII-art representation of 1 character
*/
public static void printChar(int l, char c, String[] rep, int w) {
int begin = (c-FIRST_CHAR)*w;
int end = begin+w;
System.out.print(rep[l].substring(begin, end));
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
// width and height of the ASCII-art representation of 1 character
int width = in.nextInt();
int height = in.nextInt();
// text to convert
in.nextLine();
String text = in.nextLine().toLowerCase();
// ASCII art representations of the whole alphabet + ?
String[] rows = new String[height];
for (int i = 0; i < height; i++)
rows[i] = in.nextLine();
// prints the result line by line, character by character
for (int l = 0; l < height; l++) {
for (char c : text.toCharArray())
if (FIRST_CHAR <= c && c <= LAST_CHAR)
printChar(l, c, rows, width);
else
printChar(l, QU_MARK, rows, width);
System.out.print("\n");
}
}
} ASCII Art – Python 3
Works with ord() values and slices with rep[l][begin:end]. The trick QU_MARK = ord('z') + 1 makes "?" behave like a 27th letter so the same slicing code handles it.
# some constants, ord gives the ascii code of a character (chr does the opposite)
FIRST_CHAR = ord('a')
LAST_CHAR = ord('z')
QU_MARK = LAST_CHAR+1
def printChar(l, c, rep, w):
''' prints the 'l'-th line of the ASCII-art representation of a character 'c'
rep is the ASCII-art representation of the whole alphabet + ?
and w width of the ASCII-art representation of 1 character'''
begin = (ord(c)-FIRST_CHAR)*w
end = begin+w
print(rep[l][begin:end], end="")
# width and height of the ASCII-art representation of 1 character
# text to convert, ASCII art representations of the whole alphabet + ?
width, height, text, rows = int(input()), int(input()), input().lower(), []
for i in range(height):
rows.append(input())
# prints the result line by line, character by character
for l in range(height):
for c in text:
if (FIRST_CHAR <= ord(c)) and (ord(c) <= LAST_CHAR):
printChar(l, c, rows, width)
else:
printChar(l, chr(QU_MARK), rows, width)
print()
Common mistakes
- Treating spaces as lettersA space in the text is not a letter, so it must be drawn with the "?" glyph. Skipping it, or printing L blanks, fails the tests with multi-word sentences.
- Forgetting lowercase inputTests mix "Manhattan" and "MANHATTAN". Without converting to one case, lowercase letters fall outside A-Z and are all drawn as "?".
- Trimming the font rowsCalling strip() or trim() on the font lines removes the leading spaces of glyphs like "A", which shifts every slice and garbles the whole output.
- Losing the line after nextInt()In Java, calling nextLine() right after nextInt() returns an empty string, not the text. Consume the rest of the line first, or read every line with nextLine() and parse the numbers.
Going further
The puzzle is really about index arithmetic on a flattened 2D array: the font is a grid of H rows by 27·L columns, and glyph k occupies columns k·L to k·L + L - 1. Once you see it that way, there is nothing to "parse" in the drawing: you never need to split the font into separate letter objects. A nice consequence is that the same code works for any font size, which is exactly what the hidden tests check with different L and H values. If your output looks right in the console but fails, compare character counts: every output row must be exactly N·L characters long, and a single trimmed space or a missing "?" for a digit is enough to break that invariant.
FAQ
How do I handle characters that are not letters in ASCII Art?
Anything outside A-Z after converting to uppercase, including spaces and digits, uses the 27th glyph, the "?" at index 26.
Why is my ASCII Art output shifted?
Usually the font rows were trimmed or split, which removes meaningful spaces. Keep each row exactly as read and slice it at index * L.
Should I print letter by letter or row by row?
Row by row. The console prints lines from top to bottom, so for each of the H rows you print the corresponding slice of every character of the text.