I have a file with 500,000 lines and I want to check for each line L whether any other line in the same file ends with L.
I have already sorted the file by the length of the lines and have written the following code but it is to slow:
def main(args: Array[String]): Unit = {
val buffer = new BufferedReader(new FileReader("input.txt"))
val fw = new FileWriter("output.txt")
var line = buffer.readLine()
var list = List.empty[String]
while (line != null) {
if (line.nonEmpty) {
list += line
}
line = buffer.readLine()
}
buffer.close()
list = list.sortBy(s => s.length)
for (i <- list.indices) {
val endsWith = list(i)
for (j <- i + 1 until list.size) {
val right = list(j)
if (right.endsWith(endsWith)) {
fw.write(list(j) + ";" + list(i) + "\n")
fw.flush()
}
}
println(i + 1)
}
fw.close()
}
The input file contains such entries like:
abc/defg
defg
...
Is there a more efficient way to check the lines?