HackerRank Grid challenge problem solution YASH PAL, 31 July 2024 In this HackerRank Grid challenge problem solution we have given a square grid of characters in the range ascii[a-z], rearrange elements of each row alphabetically, ascending. Determine if the columns are also in ascending alphabetical order, top to bottom. Return YES if they are or NO if they are not. Problem solution in Python. def checkLex(arr, n): for j in range(n-1): for k in range(n): if arr[j][k] > arr[j+1][k]: return False return True t = int(input().strip()) for i in range(t): n = int(input().strip()) line = [] for j in range(n): x = list(input().strip()) x.sort() line.append(x) if checkLex(line, n): print("YES") else: print("NO") {“mode”:”full”,”isActive”:false} Problem solution in Java. import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) throws IOException { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while(t-->0){ int n = Integer.parseInt(br.readLine()); String[]grid = new String[n]; for(int i=0;i<n;i++) grid[i] = sort(br.readLine()); boolean ok = true; for(int i=0;i<n;i++){ for(int j=1;j<n;j++){ if(grid[j].charAt(i) < grid[j-1].charAt(i)){ ok = false; break; } } } System.out.println(ok?"YES":"NO"); } } public static String sort(String s){ char[] array = s.toCharArray(); Arrays.sort(array); return new String(array); } } {“mode”:”full”,”isActive”:false} Problem solution in C++. #include <iostream> #include <algorithm> #include <string> using namespace std; string s[111]; int main() { int t; cin >> t; while (t--) { int n; cin >> n; for (int i = 0; i < n; i++) cin >> s[i], sort(s[i].begin(), s[i].end()); bool flag = true; for (int i = 0; i < n; i++) for (int j = 0; j + 1 < n; j++) if (s[j][i] > s[j + 1][i]) flag = false; puts(flag ? "YES" : "NO"); } return 0; } {“mode”:”full”,”isActive”:false} Problem solution in C. #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int num_testcases, number; char array[100][100]; int i, j, k, l; char c, t; int match; scanf("%d", &num_testcases); for (i = 0; i < num_testcases; i++) { scanf("%d", &number); match = 1; for (j = 0; j < number; j++) { /* no of lines */ while ((c = fgetc(stdin)) == 'n') continue; ungetc(c, stdin); for (k = 0; ((k < number) && ((c = fgetc(stdin)) != 'n')); k++) { for(l = k; l>0; l--) { if (array[j][l-1] > c) { array[j][l] = array[j][l-1]; } else { break; } } array[j][l] = c; // printf("wrote %c into [%d][%d]n", c, j, l); } } /*printf("Array read is:n"); for (l = 0; l < number; l++) { for (j = 0; j < number; j++) { printf("%c", array[l][j]); } printf("n"); } printf("n"); */ for (l = 0; l < number-1; l++) { for (j = 0; j < number-1; j++) { if ((array[l][j] > array[l][j+1]) || (array[l][j] > array[l+1][j])) { match = 0; goto done; } } } /* Check last row and last column */ for (k = 0; k < number-1; k++) { if (array[l][k] > array[l][k+1]) { match = 0; goto done; } } for (k = 0; k < number-1; k++) { if (array[k][j] > array[k+1][j]) { match = 0; goto done; } } done: if (match) { printf("YESn"); } else { printf("NOn"); } } return 0; } {“mode”:”full”,”isActive”:false} algorithm coding problems