/
EqualRationalNumbers.java
188 lines (155 loc) · 5.58 KB
/
EqualRationalNumbers.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// https://leetcode.com/problems/equal-rational-numbers/
class Solution {
public boolean isRationalEqual(String s, String t) {
RationalNumber r1 = new RationalNumber(s);
RationalNumber r2 = new RationalNumber(t);
return r1.equals(r2);
}
class RationalNumber {
int base;
String decimal;
private static final int DECIMAL_LENGTH = 20;
private static final int EXPANSION_LENGTH = DECIMAL_LENGTH + 5;
RationalNumber(String s) {
preprocess(s);
applyAdjustments();
}
private void preprocess(String s) {
// Check if there is a dot to split or not
if (!s.contains(".")) {
base = Integer.parseInt(s);
decimal = "0";
return;
}
// Split by .
String[] parts = s.split("\\.");
// First part is our base
base = Integer.parseInt(parts[0]);
// Second part will be our decimal
if (parts.length == 2) {
parseDecimal(parts[1]);
} else {
decimal = "0";
}
}
private void parseDecimal(String s) {
// s can be empty
if (s.isEmpty()) {
decimal = "0";
return;
}
// Extract the repeating and non-repeaing part
String nonRepeating = "";
String repeating = "";
// If there is no bracket, entire thing is non-repeating
int sIndex = s.indexOf("(");
if (sIndex < 0) {
nonRepeating = s;
decimal = nonRepeating;
return;
}
nonRepeating = s.substring(0, sIndex);
repeating = s.substring(sIndex + 1, s.indexOf(")"));
boolean allNines = allNines(repeating);
boolean allZeroes = allZeroes(repeating);
if (allNines && !nonRepeating.isEmpty()) {
int leadingZeroes = leadingZeroes(nonRepeating);
String added = repeat("0", leadingZeroes) + (Integer.parseInt(nonRepeating) + 1);
if (added.length() != nonRepeating.length()) {
base = base + 1;
nonRepeating = "0";
} else {
nonRepeating = added;
}
repeating = "0";
} else if (allNines) {
base = base + 1;
repeating = "0";
} else if (allZeroes && !nonRepeating.isEmpty()) {
repeating = "0";
} else if (allZeroes) {
repeating = "0";
} else {
repeating = repeat(repeating);
}
decimal = nonRepeating + repeating;
}
private int leadingZeroes(String s) {
int count = 0;
for (char c: s.toCharArray()) {
if (c == '0') {
count++;
} else {
break;
}
}
return count;
}
private String repeat(String s) {
StringBuilder sb = new StringBuilder();
while (sb.length() < EXPANSION_LENGTH) {
sb.append(s);
}
return sb.toString();
}
private String repeat(String s, int count) {
StringBuilder sb = new StringBuilder();
while (count-- > 0) {
sb.append(s);
}
return sb.toString();
}
private void applyAdjustments() {
adjustDecimalLength();
trimZeroes();
}
private void adjustDecimalLength() {
if (decimal.length() > DECIMAL_LENGTH) {
decimal = decimal.substring(0, DECIMAL_LENGTH);
}
}
private void trimZeroes() {
// Find the last non-zero index
int index = -1;
for (int i = decimal.length() - 1; i >= 0; i--) {
if (decimal.charAt(i) != '0') {
index = i;
break;
}
}
decimal = decimal.substring(0, index + 1);
}
private boolean allZeroes(String s) {
// If all are zeroes, reduce it to single digit
boolean allZeroes = true;
for (char c: s.toCharArray()) {
if (c != '0') {
allZeroes = false;
break;
}
}
return allZeroes;
}
private boolean allNines(String s) {
// If all are nines, increment to next number
boolean allNines = true;
for (char c: s.toCharArray()) {
if (c != '9') {
allNines = false;
break;
}
}
return allNines;
}
public boolean equals(Object other) {
if (!(other instanceof RationalNumber)) {
return false;
}
RationalNumber o = (RationalNumber) other;
return this.base == o.base && this.decimal.equals(o.decimal);
}
public String toString() {
return base + "." + decimal;
}
}
}