Skip to content

Latest commit

 

History

History
48 lines (43 loc) · 1.62 KB

Add_Border.md

File metadata and controls

48 lines (43 loc) · 1.62 KB

🔷 Add Border 🔷

Challenge description

Given a rectangular matrix of characters, add a border of asterisks(*) to it.

Example

For

picture = ["abc",
           "ded"]

the output should be

solution(picture) = ["*****",
                     "*abc*",
                     "*ded*",
                     "*****"]

Input/Output

  • [execution time limit] 3 seconds (java)

  • [memory limit] 1 GB

  • [input] array.string picture

    A non-empty array of non-empty equal-length strings.

    Guaranteed constraints:
    1 ≤ picture.length ≤ 100,
    1 ≤ picture[i].length ≤ 100.

  • [output] array.string

    The same matrix of characters, framed with a border of asterisks of width 1.

[Java] Syntax Tips

## Solutions:
  • JS solution

    function solution(array) {
    let solution = new Array()
    for (let i = 0; i < array.length; i++)
    solution [i + 1] = "*" + array[i] + "*"
    let border = ""
    for(; border.length < array[0].length; border += "*"){}
    solution[0] = solution[solution.length] = border += "**"
    return solution
    }
    JS Execution

  • Java solution

    static String[] solution(String[] picture){
    String[] solution = new String[picture.length + 2];
    for (int i = 0; i < picture.length; i++)
    solution[i + 1] = "*" + picture[i] + "*";
    String border = "";
    for(; border.length() < picture[0].length() + 2; border += '*'){}
    solution [0] = solution[solution.length - 1] = border;
    return solution;
    }
    Java Execution