Created
October 23, 2025 16:28
-
-
Save tatsuyax25/442c3232068d33e292d7bfda6e5e5640 to your computer and use it in GitHub Desktop.
You are given a string s consisting of digits. Perform the following operation repeatedly until the string has exactly two digits: For each pair of consecutive digits in s, starting from the first digit, calculate a new digit as the sum of the two d
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {string} s | |
| * @return {boolean} | |
| */ | |
| var hasSameDigits = function(s) { | |
| // Convert the input string into an array of digits (as numbers) | |
| let digits = s.split('').map(Number); | |
| // Repeat the transformation until only two digits remain | |
| while (digits.length > 2) { | |
| let nextDigits = []; | |
| // Loop through each pair of consecutive digits | |
| for (let i = 0; i < digits.length - 1; i++) { | |
| // Compute the sum modulo 10 | |
| let sumMod10 = (digits[i] + digits[i + 1]) % 10; | |
| nextDigits.push(sumMod10); // Store the result | |
| } | |
| // Update the digits array with the newly computed values | |
| digits = nextDigits; | |
| } | |
| // Final check: are the last two digits equal? | |
| return digits[0] === digits[1]; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment