Check whether Lower triangular matrix & Upper triangular matrix has same characters

Given a 2D character array, check if all the entries which are on and above the diagonal of the matrix are equal to all the entries which are on and below the diagonal of the matrix.


#include <stdio.h>
#include <stdlib.h>
#define M 5
#define N 5

/* function to compute the hash table for the upper triangular matrix */
void computeupperhash(char *ch[],int *uhash)
{
 int i,j;
 for(i=0;i<M;i++)
 {
  for(j=0;j<N;j++)
  {
   if(i<j)
    uhash[*(*(ch+i)+j)-'a']++;
  }
 }
}

/* compute the hash table for lower triangular matrix */
void computelowerhash(char *ch[],int *lhash)
{
 int i,j;
 for(i=0;i<M;i++)
 {
  for(j=0;j<N;j++)
  {
   if(i>j)
    lhash[*(*(ch+i)+j)-'a']++;
  }
 }
}

/* check whether the values of two hash tables are same,
if there is any one match , return false
else if all the characters matches, return true */
int checkhashes(int *a,int *b)
{
 int i;
 for(i=0;i<256;i++)
 {
  if(*(a+i)!=*(b+i))
  {
   return 0;
  }
 }
 return 1;
}

int main(void) {
 char *ch[M];
 int uhash[256],lhash[256];
 int i,j;
 
 /* initialize the hash tables to zero */
 memset(uhash,0,sizeof(uhash));
 memset(lhash,0,sizeof(lhash));
 
 /* dynamically allocate the memory block for 2-D array of characters */
 for(i=0;i<M;i++)
  *(ch+i)=(char*)malloc(sizeof(char)*256);
 
 /* get the input 2-D array of characters */
 for(i=0;i<M;i++)
 {
  for(j=0;j<N;j++)
  {
   scanf("%c",(*(ch+i)+j));
  }
 }
 
 /* computer the hash table for upper triangular matrix 
 with the count of characters present */
 computeupperhash(ch,uhash);
 
 /* compute the hash table for lower triabgular matrix 
 with the count of characters present */
 computelowerhash(ch,lhash);
 
 /* print the hash tables */
 for(i=0;i<26;i++)
 {
  printf("%d %d\n",*(uhash+i),*(lhash+i));
 }
 
 /* check if the count of characters present in the upper triangular matrix
 & the characters in the lower triangular matrix are same */
 if(checkhashes(uhash,lhash))
  printf("\nYES");
 else
  printf("\nNO");
 return 0;
}