top of page

Edabit Java: Incrementing Rows and Columns, Very Hard Difficulty


Solving this question in Edabit: https://edabit.com/challenge/jvyJ6xmPyrWab2MeX using the pattern regex matcher. it was a tough question!


Question:




Answer:



import java.util.Arrays;

import java.util.regex.Matcher;

import java.util.regex.Pattern;


public class RowsAndColumns {

public static int[][] increment(int r, int c, String[] letters) {

int[][] nums = new int[r][c];

int columnOrRow = 0;

String number = "";


Pattern p = Pattern.compile("\\d+");


for (int i = 0; i < letters.length; i++) {

Matcher m = p.matcher(letters[i]);

number = "";

while(m.find()) {

number += m.group();

}

columnOrRow = Integer.parseInt(number);

if (letters[i].contains("r")) {

for (int j = 0; j < nums[0].length; j++) {

nums[columnOrRow][j] += 1;

}


} else {

for (int j = 0; j < nums.length; j++) {

nums[j][columnOrRow] += 1;

}

}

}

return nums;

}

}


3 views0 comments
Post: Blog2_Post
bottom of page