Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.

I. Problem description

Implement an algorithm that determines whether all characters of a string s are different.

Title link: Determine if a character is unique.

Two, the title requirements

Sample 1

Input: s = "leetcode" Output: falseCopy the code

The sample 2

Input: s = "ABC" Output: trueCopy the code

inspection

2. The recommended time is 15 to 35 minutesCopy the code

Third, problem analysis

In this article, you can read more detailed information about bitwise operation:

Day 45: Bit operation

1. The string

At first, I thought this was a real problem and I didn’t think about how to combine characters with bits.

I will in accordance with the usual ideas to solve, mainly using C++ standard template library String class function, not understand the String class function can see this article: algorithm daily exercise 22 days: guess letters.

To determine if it is unique, just do the following two steps:

1. Store a single letter in the string to k. 2Copy the code

2. Bit operations

Just like the rest of the problem, bit operations are not very advantageous for this problem. The code is also difficult to understand, so I’m not going to write the bit operation code, lay flat.

Four, coding implementation

class Solution {
public:
    bool isUnique(string astr) {
        string k;// the intermediate variable k
        int i,n=astr.size(a);// Initialize the definition
        for(i=0; i<n; i++)/ / a for loop
        {
            k=astr[i];/ / assignment
            if(astr.find(k)! =- 1&&astr.find(k)! =i)// Determine if there are any duplicate elements
            {
                return false;// Return error}}return true;// Return correct}};Copy the code

V. Test results