[C++/Java] Bitwise Operations for Efficient Digit Tracking
We the process by concatenating n, 2*n, and 3*n. Following this, it verifies that the concatenated string\'s length is strictly 9. If not, it directly returns f
We the process by concatenating `n`, `2*n`, and `3*n`. Following this, it verifies that the concatenated string\'s length is strictly `9`. If not, it directly returns `false`, as it\'s not possible for the number to incorporate all digits from `1` to `9` individually.\n\nSubsequently, a `state` variable is defined, initialized to `0`. This will be used to track each digit from `1` to`9` in the concatenated number. The function proceeds with a loop, examining each character in the string, and performing two checks:\n\n* If the character equals `0`, the function directly returns false. This is because a fascinating number should not include any zeroes.\n\n* We also check whether the same digit has been encountered previously using bitwise operations. If the current digit\'s corresponding bit in the `state` variable is already 1, it denotes that the digit has been encountered before, and the function immediately returns false.\n\nIn the event that neither of these conditions is met, the function adjusts the `state` variable, setting the current digit\'s corresponding bit to `1` using bitwise operations.\n\nC++:\n```\nclass Solution {\npublic:\n    bool isFascinating(int n) {\n        string s = to_string(n) + to_string(2 * n) + to_string(3 * n);\n        if (s.length() != 9) return false;\n        int state = 0;\n        for (int i = 0; i < s.length(); i++) {\n            int c = s[i] - \'0\';\n            if (c == 0 || ((state >> c) & 1) == 1) return false;\n            state |= (1 << c);\n        }\n        return true;\n    }\n};\n```\n\nJava:\n```\nclass Solution {\n    public boolean isFascinating(int n) {\n        StringBuilder sb = new StringBuilder();\n        sb.append(n).append(2 * n).append(3 * n);\n        if (sb.length() != 9) return false;\n        int state = 0;\n        for (int i = 0; i < sb.length(); i++) {\n            int c = sb.charAt(i) - \'0\';\n            if (c == 0 || ((state >> c) & 1) == 1) return false;\n            state |= (1 << c); \n        }\n        return true;\n    }\n}\n```
['C', 'Java']