Cover image for LeetCode Daily Problem P3461 Check if Numbers in String are Equal After Operations I

LeetCode Daily Problem P3461 Check if Numbers in String are Equal After Operations I


Timeline

timeline

2025-10-23

init

simulation

Problem:

This is Pascal’s triangle. Here we can directly simulate it. Use a queue to implement. Note that we need to distinguish the first and the last, because these two numbers are used only once in the addition and modulo operation. Therefore we use -1 as a marker. It indicates that the number before -1 is the last number.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <string>
#include <queue>
using std::string;
using std::queue;

class Solution {
public:
bool hasSameDigits(string s)
{
queue<int> que;
int val1, val2;
for (char ch : s) {
que.push(ch - '0');
}
que.push(-1);

while (que.size() > 3) {
val1 = que.front();
que.pop();

val2 = que.front();
if (val2 == -1) {
que.pop();
que.push(-1);
continue;
}
que.push((val1 + val2) % 10);
}

if (que.size() == 3) {
val1 = que.front();
que.pop();
val2 = que.front();
if (val1 == val2) {
return true;
}
}
return false;
}
};