Longest common subsequence + substring

Need to free memory later

Add one to array lengths when calling

void LongestCommonSubsequence(int *array1, int *array2, int arrayLength1, int arrayLength2)
{
int **table = malloc(arrayLength1 * sizeof(int*));
for(int i = 0; i < arrayLength1; i++)
{
table[i] = malloc(arrayLength2 * sizeof(int));
table[i][0] = 0;
}
for(int i = 0; i < arrayLength2; i++)
{
table[0][i] = 0;
}

for(int i = 1 ; i < arrayLength1; i++)
{
for(int j = 1; j < arrayLength2; j++)
{
if(array1[i-1] == array2[j-1])
{
table[i][j] = table[i-1][j-1] + 1;
}
else if(table[i-1][j] >= table[i][j-1])
{
table[i][j] = table[i-1][j];
}
else
{
table[i][j] = table[i][j-1];
}
}
}

int i = arrayLength1-1;
int j = arrayLength2-1;
int length = table[i][j];
int length2 = table[i][j];
int *longest = malloc(length * sizeof(int));
while(i > 0 && j > 0)
{
if(array1[i-1] == array2[j-1])
{
longest[length-1] = array1[i-1];
i--;
j--;
length--;
}
else if(table[i-1][j] > table[i][j-1])
{
i--;
}
else
{
j--;
}
}

for(int k = 0; k < length2-1; k++)
{
printf("%d ",longest[k]);
}
}
//Substring

void Common(int *array1, int *array2, int length1, int length2)
{
int **table = malloc((length1 + 1) * sizeof(int*));
for(int i = 0; i < length1+1; i++)
{
table[i] = calloc(length2+1, sizeof(int));
}
int stringLength = 0;
int maxRow = 0;
int maxColumn = 0;
for(int i = 0; i < length1 + 1; i++)
{
for(int j = 0; j < length2 + 1; j++)
{
if(i == 0 || j == 0)
{
table[i][j] = 0;
}
else if(array1[i-1] == array2[j-1])
{
table[i][j] = table[i-1][j-1] + 1;
if(stringLength < table[i][j])
{
stringLength = table[i][j];
maxRow = i;
maxColumn = j;
}
}
else
{
table[i][j] = 0;
}
}
}

if(stringLength == 0)
{
printf("No matching string");
}
else
{
int max = stringLength;
int *common = malloc(sizeof(int) * stringLength);
while(table[maxRow][maxColumn] != 0)
{
common[--stringLength] = array1[maxRow - 1];
maxRow--;
maxColumn--;
}
printf("Start: (%d %d)", maxRow,max);
for(int i = 0; i < max; i++)
{
printf("%d ",common[i]);
}
free(common);
}
for(int i = 0; i < length1+1; i++)
{
free(table[i]);
}
free(table);
}