Talentd
FleetCode

3463. Check If Digits Are Equal in String After Operations II

Hard14.3% acceptance2 companies

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 digits modulo 10.
  • Replace s with the sequence of newly calculated digits, maintaining the order in which they are computed.

Return true if the final two digits in s are the same; otherwise, return false.

Example 1:

Input: s = "3902"

Output: true

Explanation:

  • Initially, s = "3902"
  • First operation:
    • (s[0] + s[1]) % 10 = (3 + 9) % 10 = 2
    • (s[1] + s[2]) % 10 = (9 + 0) % 10 = 9
    • (s[2] + s[3]) % 10 = (0 + 2) % 10 = 2
    • s becomes "292"
  • Second operation:
    • (s[0] + s[1]) % 10 = (2 + 9) % 10 = 1
    • (s[1] + s[2]) % 10 = (9 + 2) % 10 = 1
    • s becomes "11"
  • Since the digits in "11" are the same, the output is true.

Example 2:

Input: s = "34789"

Output: false

Explanation:

  • 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.

Constraints:

  • 3 <= s.length <= 105
  • s consists of only digits.

Asked at 2 companies

Hints open one at a time

Hint 1
Can we use nCr and use Pascal's triangle values here?
Hint 2
nCr mod 10 can be uniquely determined from nCr mod 2 and nCr mod 5.
Hint 3
Use Lucas's theorem.

Similar problems

Pascal's TriangleEasy
Loading editor…
1class Solution:
2 def hasSameDigits(self, s: str) -> bool:
3
Starter codePython 3Spaces: 4
Examples
Input
"3902"
Input
"34789"
Your code is saved on this device.