[LeetCode] Check if The Number is Fascinating

2729. Check if The Number is Fascinating

You are given an integer n that consists of exactly 3 digits.

We call the number n fascinating if, after the following modification, the resulting number contains all the digits from 1 to 9 exactly once and does not contain any 0‘s:

  • Concatenate n with the numbers 2 * n and 3 * n.

Return true if n is fascinating, or false otherwise.

Concatenating two numbers means joining them together. For example, the concatenation of 121 and 371 is 121371.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
bool isFascinating(int n) {
string a = to_string(n), b = to_string(2 * n), c = to_string(3 * n);
vector<int> freq(10);
for(auto ch : a) freq[ch-'0'] += 1;
for(auto ch : b) freq[ch-'0'] += 1;
for(auto ch : c) freq[ch-'0'] += 1;
for(int i = 1; i <= 9; i++) if(freq[i] != 1) return false;
return freq[0] == 0;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2023/06/11/PS/LeetCode/check-if-the-number-is-fascinating/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.