Grid Challenge – HackerRank Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
Solutions of Algorithms Data Structures Hard HackerRank:
Here are all the Solutions of Hard , Advanced , Expert Algorithms of Data Structure of Hacker Rank , Leave a comment for similar posts
1 Month Preparation Kit Solutions – HackerRank
C++ Grid Challenge HackerRank Solution
#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;
}
Java Grid Challenge HackerRank Solution
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);
}
}
Python 3 Grid Challenge HackerRank Solution
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")
JavaScript Grid Challenge HackerRank Solution
C Grid Challenge HackerRank Solution
#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("YES\n");
} else {
printf("NO\n");
}
}
return 0;
}
Leave a comment below