[problem solving report] (Lecture 24) string algorithm - character counting method

catalogue

Zero, write in front

1, Main knowledge points

         1. Character counting

2, After class exercises  

Interview question 01.01. Determine whether the character is unique

Sword finger Offer 50. The first character that appears only once

383. Ransom letter

771. Gemstones and stones

  Interview question 01.02. Determine whether it is character rearrangement

1941. Check that all characters appear the same number of times

242. Valid acronyms

  Sword finger Offer II 032. Effective modifier

  1832. Judge whether the sentence is a full letter sentence

2053. The K-th unique string in the array

  Write at the end

Zero, write in front

         This is the 24th day of clocking in. There are many topics today. The main knowledge points are

Lecture 100 on fundamentals of zero algorithm (Lecture 24) string algorithm (IV) - character counting methodhttps://blog.csdn.net/WhereIsHeroFrom/article/details/121295716

1, Main knowledge points

         1. Character counting

        The hash table can be used to record the number of strings. If the string contains only lowercase letters, it can be further compressed.

    int hash[26] ={0};    //hash table incidental initialization
    for(int i = 0;s[i];++i)
        hash[s[i] - 'a']++;    //Statistical quantity

2, After class exercises  

Interview question 01.01. Determine whether the character is unique

Interview question 01.01. Determine whether the character is uniquehttps://leetcode-cn.com/problems/is-unique-lcci/ thinking

Using the hash table, if the scanned letters are scanned, it will directly return false, and if it is not encountered after all scanning, it will return true.

bool isUnique(char* astr){
    bool hash[256];
    memset(hash,0,sizeof(hash));
    for(int i = 0;astr[i];++i)
        if(!hash[astr[i]])  hash[astr[i]]++;
        else return false;
    return true;
}

Result analysis

Make do with

Sword finger Offer 50. The first character that appears only once

Sword finger Offer 50. The first character that appears only oncehttps://leetcode-cn.com/problems/di-yi-ge-zhi-chu-xian-yi-ci-de-zi-fu-lcof/ thinking

First use the hash table to count the occurrence times of all letters, and then scan the hash table. If there is one, it will be returned

Returns a space if there is no.

This question returns a character! So you can directly return a value!

char firstUniqChar(char* s){
    int hash[26] ={0};    //hash table + initialization
    for(int i = 0;s[i];++i)    //Record hash value
        hash[s[i] - 'a']++;
    for(int i = 0;s[i];++i)    //Traversal hash table
        if(hash[s[i] - 'a'] == 1)   return s[i];
    return ' ';    //Return space not found
}

Result analysis

It's past

383. Ransom letter

383. Ransom letterhttps://leetcode-cn.com/problems/ransom-note/ thinking

This time, it's just a different way to update the hash table

First, scan the magazine and insert all the elements and numbers into the hash table

Then, scan ransom to one character and subtract the corresponding hash table value by 1

bool canConstruct(char * ransomNote, char * magazine){
    int hash[256] = {0};    //hash table
    for(int i = 0;magazine[i];++i)//Scan magazine update hash table
        hash[magazine[i]] ++;
    for(int i = 0;ransomNote[i];++i)    //Scan ransom update hash table
        if(!hash[ransomNote[i]])  return false;//Not enough corresponding elements?
        else hash[ransomNote[i]]--;
    return true;
}

Result analysis

 It's OK

771. Gemstones and stones

771. Gemstones and stoneshttps://leetcode-cn.com/problems/jewels-and-stones/ thinking

First scan all elements in J, and then judge the elements in s according to the hash table for statistics.

int numJewelsInStones(char * jewels, char * stones){
    bool has[256] = {0};    //hash table
    int ans = 0;
    for(int i = 0; jewels[i]; ++i)    //Update hash table
        if(has[jewels[i]] == 0) has[jewels[i]] = 1;
    for(int i = 0; stones[i]; ++i)//According to hash statistics
        if(has[stones[i]] == 1) ans++;
    return ans;
}

Result analysis

fairish

  Interview question 01.02. Determine whether it is character rearrangement

Interview question 01.02. Determine whether it is character rearrangementhttps://leetcode-cn.com/problems/check-permutation-lcci/ thinking

If the number of corresponding elements is the same and the length of two strings is the same, the condition is satisfied.

bool CheckPermutation(char* s1, char* s2){
    int hash[256] = {0},count = 0;//hash table
    for(int i = 0;s1[i];++i){    //View all elements of s1
        hash[s1[i]]++;
        count++;
    }
    for(int i = 0;s2[i];++i){    //Compare whether the elements in s2 and s1 are the same
        if(hash[s2[i]])  hash[s2[i]]--;//Found return
        else return false;
        if(!(count--)) return false;
    }
    if(count == 0)return true;//Equal length
    else return false;    //s1 is longer than s2
}

Result analysis

It's OK

1941. Check that all characters appear the same number of times

1941. Check that all characters appear the same number of timeshttps://leetcode-cn.com/problems/check-if-all-characters-have-equal-number-of-occurrences/ thinking

First save the occurrence times of all elements in the hash table, and then check whether the non-0 elements in the hash table are equal to the maximum value.

bool areOccurrencesEqual(char * s){
    int max = 0,hash[26] = {0};//Initialization of hash table and maximum value
    for(int i = 0;s[i];++i){    //statistical information 
        hash[s[i]-'a']++;
        if(hash[s[i]-'a'] > max) max = hash[s[i]-'a'];
    }
    for(int i = 0;i < 26;i++)    //See if the requirements are met
        if(hash[i] && hash[i] != max)  return false;
    return true;
}

Result analysis

Make do with

242. Valid acronyms

242. Valid acronymshttps://leetcode-cn.com/problems/valid-anagram/ thinking

Go in two steps.

First determine whether all elements have

Then judge whether the length is the same

bool isAnagram(char * s, char * t){
    int hash[26] = {0};
    for(int i = 0;s[i];++i)    //Insert hash statistics
        hash[s[i] - 'a']++;
    for(int i = 0;t[i];++i)    //See if all elements are enough
        if(hash[t[i] - 'a'])    hash[t[i]-'a']--;
        else    return false;
    for(int i = 0;i < 26;++i)
        if(hash[i]) return false;//The hash table also has values indicating different lengths
    return true;
}

Result analysis

Make do with

  Sword finger Offer II 032. Effective modifier

Sword finger Offer II 032. Effective modifierhttps://leetcode-cn.com/problems/dKk3P7/ thinking

Go in two steps

Count whether the number of occurrences of each character is the same

Determine whether the length is equal or identical

bool isAnagram(char * s, char * t){
    int hash[26] = {0};
    int ssize= 0,tsize = 0;
    for(ssize = 0;s[ssize];++ssize)//Statistical words
        hash[s[ssize] - 'a']++;
    for(tsize = 0;t[tsize];++tsize)//See if the words appear the same number of times
        if(hash[t[tsize] - 'a'])    hash[t[tsize]-'a']--;
        else    return false;
    if(ssize != tsize || !strcmp(s,t))   return false;    //Are the lengths the same and not exactly the same
    return true;
}

Problem solving ideas

Make do with

  1832. Judge whether the sentence is a full letter sentence

1832. Judge whether the sentence is a full letter sentencehttps://leetcode-cn.com/problems/check-if-the-sentence-is-pangram/ thinking

First, use the hash table to count all the letters that appear,

Just scan the hash table to see if there are any letters that don't appear?

bool checkIfPangram(char * sentence){
    char hash[26] = {0};
    for(int i = 0;sentence[i];++i)//View the elements that appear
        hash[sentence[i] - 'a'] = true;
    for(int i = 0;i < 26;i++)    //See if there are elements that do not appear
        if(!hash[i])    return false;
    return true;
}

Problem solving ideas

Starfish

2053. The K-th unique string in the array

2053. The K-th unique string in the arrayhttps://leetcode-cn.com/problems/kth-distinct-string-in-an-array/ thinking

Use a character pointer array to save different strings

When scanning the same character, the corresponding hashnum value will be + 1

Scan the entire hash table to find the hashnum value that only appears once, find a k-1, and finally return the corresponding hash value. If it cannot be found, an empty string of an application will be returned

char * kthDistinct(char ** arr, int arrSize, int k){
    int count = 0,ansnum = 0;
    char * hash[1000];    //Collect different strings
    int hashnum[1000]= {0};    //Collect the number of occurrences of the corresponding string
    for(int i = 0;i < arrSize;i++){    //Scan all ARRS for hash and hashnum insertion
        int j = 0;
        for(j = 0;j < count;j++)
            if(strcmp(hash[j],arr[i]) == 0){//Appeared
                hashnum[j] ++;
                break;
            }
        if(j == count) {        //Not present
            hash[count++] = arr[i];
            hashnum[count - 1] ++;
        }
    }
    for(int i = 0,j = k;j > 0 && i < count;i++){    //Find the corresponding string address
        if(hashnum[i] == 1)j--;
        if(j == 0)  return hash[i];
    }
    char *s = malloc(sizeof(char));//Empty string must be applied
    s[0] = 0;
    return s;
}

Result analysis

Finished, finally!

  Write at the end

If you don't want to stay in the bedroom, you must stay away from the bed. It's winter. Keep warm and don't catch a cold!

The win11 of the tablet is still short of an acpi. I have to repair it again, but it's turned on. It's the blue screen. Woo woo.

Tags: C Algorithm data structure

Posted on Fri, 12 Nov 2021 22:53:10 -0500 by consolestrat