Skip to content
Programmingoneonone
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

  • Home
  • CS Subjects
    • IoT ? Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

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.

HackerRank Grid challenge problem solution

Topics we are covering

Toggle
  • Problem solution in Python.
  • Problem solution in Java.
  • Problem solution in C++.
  • Problem solution in C.

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}

Algorithms coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes