Wrote java solution