medium
0 views

Mirror of L Pattern

Print a pattern where each line shows repeated characters from the start of the string followed by the remaining characters

Understand the Problem

Problem Statement

The program must accept a string S as the input. The program must print the desired pattern as shown in the Example Input/Output section.

Constraints

  • 3 ≤ Length of S ≤ 100
  • String S contains only lowercase alphabets
  • Output pattern must match exact format shown in examples

Examples

Example 1
Input
skillrack
Output
skillrack
kkillrack
iiillrack
lllllrack
lllllrack
rrrrrrack
aaaaaaack
cccccccck
kkkkkkkkk
Explanation

Each line shows the character at that position repeated according to its 1-based index, followed by the remaining characters. For example, line 3 shows 'i' repeated 3 times followed by 'llrack'.

Example 2
Input
huge
Output
huge
uuge
ggge
eeee
Explanation

Line 1: h (1 time) + "uge" = "huge" Line 2: u (2 times) + "ge" = "uuge" Line 3: g (3 times) + "e" = "ggge" Line 4: e (4 times) = "eeee"

Solution

#include <stdio.h>
#include <string.h>

int main() {
    char s[101];
    fgets(s, sizeof(s), stdin);
    
    // Remove newline if present
    int len = strlen(s);
    if (s[len-1] == '\n') {
        s[len-1] = '\0';
        len--;
    }
    
    for (int i = 0; i < len; i++) {
        // Print character s[i] repeated (i+1) times
        for (int j = 0; j <= i; j++) {
            printf("%c", s[i]);
        }
        
        // Print remaining characters
        for (int k = i+1; k < len; k++) {
            printf("%c", s[k]);
        }
        
        printf("\n");
    }
    
    return 0;
}
Time:O(n²) where n is the length of string. For each position i, we print up to n characters.
Space:O(1) - only using a fixed-size character array for input storage
Approach:

C Solution Logic:

  1. Read the input string using fgets()
  2. Remove trailing newline character if present
  3. Outer loop iterates through each character position i (0 to n-1)
  4. First inner loop prints the character at position i, repeated i+1 times
  5. Second inner loop prints the remaining characters from position i+1 to end
  6. Print newline after each line

The solution uses simple nested loops with direct character-by-character printing.

Visual Explanation

Loading diagram...