-
Notifications
You must be signed in to change notification settings - Fork 7
/
grid-challenge.c
63 lines (62 loc) · 977 Bytes
/
grid-challenge.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//https://www.hackerrank.com/challenges/grid-challenge
//mandeep singh @msdeep14
#include<stdio.h>
int main()
{
int i,j,k,t,n,flag=0;
char str[100][100];
scanf("%d",&t);
while(t--)
{
flag=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s",str[i]);
}
//sort the str row wise from column to column
//insertion sort;
for(k=0;k<n;k++)
{
char current;
for(i = 1; i< n; i++)
{
current = str[k][i];
for(j = i-1; j>=0; j--)
{
if(current < str[k][j])
str[k][j+1] = str[k][j];
else
break;
}
str[k][j+1] = current;
}
}
//now check for rows if they are sorted or not;
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(str[j][i]<=str[j+1][i])
{
//do nothing;
}
else
{
flag=1;
}
}
if(flag==1)
{
break;
}
}
if(flag==0)
{
printf("YES\n");
}
else
printf("NO\n");
}
return 0;
}