Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions 3461. Check If Digits Are Equal in String After Operations I
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public:
bool hasSameDigits(string s) {
int iteration = 0; // Tracks how many layers of reduction have been done

// Continue reducing until only two digits remain
while (s.size() - iteration != 2) {
// Replace each character with the sum of adjacent digits (mod 10)
for (int i = 0; i < s.size() - 1 - iteration; i++) {
s[i] = ((s[i] - '0') + (s[i + 1] - '0')) % 10 + '0';
}
iteration++;
}

// Return true if the final two digits are equal
return s[0] == s[1];
}
};
Loading