Designer Door Mat

  • + 0 comments

    Solution for Designer Door Mat Problem

    Problem

    We need to print a door mat design pattern given two inputs n (rows) and m (columns).
    - The pattern must have WELCOME centered in the middle row.
    - The top half and bottom half must be symmetric with motifs made of .|..
    - The total width m is guaranteed to be 3 * n.


    My Approach

    1. Top Half:

      • For each row before the middle, the motif count grows as (2 * height + 1).
      • Each line is padded with - on both sides to keep the width = m.
    2. Middle Row:

      • Simply print WELCOME centered with dashes.
    3. Bottom Half:

      • Mirror the top half.
      • Motif count decreases as (2 * ((n-1) - height) + 1).
    4. Symmetry Guarantee:

      • By controlling motif counts in both halves, the design is symmetric around the middle.

    Code

    # Enter your code here. Read input from STDIN. Print output to STDOUT
    n, m = map(int, input().split())
    
    dash = '-'
    center = '.|.'
    welcome = 'WELCOME'
    
    dash_mul_top = 3 * (n // 2)
    dash_mul_bottom = 3
    
    for height in range(0, n):
        if height < n // 2:
            # Top half
            print(dash * dash_mul_top, end="")
            print(center * (2 * height + 1), end="")
            print(dash * dash_mul_top, end="")
            dash_mul_top -= 3
            print()
    
        elif height == n // 2:
            # Middle row
            dash_mul_middle = (m - len(welcome)) // 2
            print(dash * dash_mul_middle, end="")
            print(welcome, end="")
            print(dash * dash_mul_middle, end="")
            print()
    
        else:
            # Bottom half
            print(dash * dash_mul_bottom, end="")
            print(center * (2 * ((n - 1) - height) + 1), end="")
            print(dash * dash_mul_bottom, end="")
            dash_mul_bottom += 3
            print()
    

    Example

    Input:

    7 21
    

    Output:

    ---------.|.---------
    ------.|..|..|.------
    ---.|..|..|..|..|.---
    -------WELCOME-------
    ---.|..|..|..|..|.---
    ------.|..|..|.------
    ---------.|.---------
    

    Complexity Analysis

    • Time Complexity:

      • We print exactly n lines, and each line is of length m.
      • Complexity = O(n * m) (printing dominates).
    • Space Complexity:

      • Only a few counters and string multiplications are used.
      • No extra storage proportional to n or m.
      • Complexity = O(1) auxiliary space.

    ✅ The solution is clean, efficient, and guaranteed to run within constraints.