Skip to content

Commit b1db50b

Browse files
authored
Create 3461. Check If Digits Are Equal in String After Operations I (#915)
2 parents adbb779 + 7cba3b1 commit b1db50b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
bool hasSameDigits(string s) {
4+
int iteration = 0; // Tracks how many layers of reduction have been done
5+
6+
// Continue reducing until only two digits remain
7+
while (s.size() - iteration != 2) {
8+
// Replace each character with the sum of adjacent digits (mod 10)
9+
for (int i = 0; i < s.size() - 1 - iteration; i++) {
10+
s[i] = ((s[i] - '0') + (s[i + 1] - '0')) % 10 + '0';
11+
}
12+
iteration++;
13+
}
14+
15+
// Return true if the final two digits are equal
16+
return s[0] == s[1];
17+
}
18+
};

0 commit comments

Comments
 (0)