Input: s ="34789"Output: falseExplanation:
* Initially,`s = "34789"`.* After the first operation,`s = "7157"`.* After the second operation,`s = "862"`.* After the third operation,`s = "48"`.* Since `'4' != '8'`, the output is`false`.
We repeatedly reduce the string by replacing each pair of consecutive digits with their sum modulo 10, until only two digits remain. If the final two digits are equal, return true; otherwise, return false. This is a direct simulation of the process described in the problem.
classSolution {
publicbooleanisEqual(String s) {
int n = s.length();
int[] arr =newint[n];
for (int i = 0; i < n; ++i) arr[i]= s.charAt(i) -'0';
while (n > 2) {
int[] nxt =newint[n-1];
for (int i = 0; i+1 < n; ++i) nxt[i]= (arr[i]+ arr[i+1]) % 10;
arr = nxt;
n--;
}
return arr[0]== arr[1];
}
}
1
2
3
4
5
6
7
8
9
classSolution {
funisEqual(s: String): Boolean {
var arr = s.map { it - '0' }
while (arr.size > 2) {
arr = arr.zipWithNext { a, b -> (a + b) % 10 }
}
return arr[0] == arr[1]
}
}
1
2
3
4
5
6
classSolution:
defisEqual(self, s: str) -> bool:
arr = [int(c) for c in s]
while len(arr) >2:
arr = [(arr[i] + arr[i+1]) %10for i in range(len(arr)-1)]
return arr[0] == arr[1]