Cover image for LeetCode Daily Problem P3461 Check if Digits Are Equal in String After Operations I

LeetCode Daily Problem P3461 Check if Digits Are Equal in String After Operations I


Timeline

Timeline

2025-10-23

init

Simulation

Problem:

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

12345678910111213141516171819202122232425262728293031323334353637383940
#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;	}};
Loading comments…