Because combinations are not very many, the problem can be solved by exhaustive search. I'll give the code in C ++, but I think it can be easily adapted for Java:
#include <iostream> #include <iomanip> class Time { public: Time(int h, int m) : h_(h), m_(m) {} bool operator==(const Time& t) const { return h_ == t.h_ && m_ == t.m_; } bool operator!=(const Time& t) const { return !(*this == t); } Time& operator++() { if (++m_ == 60) { m_ = 0; if (++h_ == 24) { h_ = 0; } } return *this; } bool isSymetric() const { int d1 = h_ / 10; int d2 = h_ % 10; int d3 = m_ / 10; int d4 = m_ % 10; return d1 == d4 && d2 == d3; } int h() const { return h_; } int m() const { return m_; } private: int h_; int m_; }; std::ostream& operator<<(std::ostream& os, const Time& time) { char prev = os.fill('0'); os << std::setw(2) << time.h() << ':' << std::setw(2) << time.m(); os.fill(prev); return os; } int main() { Time time(0, 0); do { if (time.isSymetric()) { std::cout << time << std::endl; } } while (++time != Time(0, 0)); }
Result:
00:00 01:10 02:20 03:30 04:40 05:50 10:01 11:11 12:21 13:31 14:41 15:51 20:02 21:12 22:22 23:32