Coded a solution in Java.

Need to convert to Python.