My Project Purpose : There are 4 files. Each of them has different number of lines. Each line consists of a single or few words. Now, for each of these files, I want to count which other file has the maximum common words.
- eg. (',' is a new line )
- Input:
- File 1 Lines : A,B,C,D
- File 2 Lines : C,D,E,F
- File 3 Lines : A,E,C,G
File 4 Lines : C,E,F,A
Output :
- File 1 : Maximum common words is 2 and they are in the files : File 2 (C,D), File 3 (A,C) and File 4 (C,A).
- File 2 : Maximum common words is 3 and they are in the files : File 4 (C,E,F).
- File 3 : Maximum common words is 3 and they are in the files : File 4 (C,E,A).
- File 4 : Maximum common words is 3 and they are in the files : File 2 (C,E,F).
My logic :
- Start
- Read each line from file and store it in memory as a 1-D array (eg. array1[0] = "A", array1[1] = "B" and so on.
- Since there are 4 files, I create 4 arrays = array1 to array4. Each of them will have the contents of their corresponding files.
- Now I will compare the first words in the first array with the first word in the second array.
- Now I will compare the first words in the first array with the second word in the second array and so on till the end on second array.
- I will continue this till the last word in the last array.
- When ever I found something was matching I will note down in a variable by incrementing 1.
Wish to know if this is the right of approaching this problem.
Or is there a better way to think at this problem?
Edits : 1. Forgot to add, Will be using php.