Data structure and algorithm -- hash table and string

Posted by Hardwarez on Sun, 30 Jan 2022 14:21:59 +0100

1. Preparatory knowledge

1.1 simplest hash - count the number of characters

1. Title Description

Enter a string and output the number of each character in the string
For example: simple_hash(“abcdefgaaxxy”)
Output:
[a][97]:3
[b][98]:1
[c][99]:1
[d][100]:1
[e][101]:1
[f][102]:1
[g][103]:1
[x][120]:2
[y][121]:1

2.C + + code implementation

class solution {
public:
	//1. The simplest hash is to input the string and output the number of repeated characters in the string
	void simple_hash(string str){
		int char_map[128] = { 0 };
		
		for (int i = 0; i < str.length(); i++) {
			char_map[str[i]]++;
		}
		for (int i = 0; i < 128; i++) {
			if (char_map[i] > 0) {
				printf("[%c][%d]:%d\n", i, i, char_map[i]);
			}
		}
	}
};

1.2 hash table sorting integer

1. Title Description

Input: {999, 1444, 7, 20, 9, 1, 3, 7, 7}
Output: 1,1, 3, 7,7,7,9444999

2.C + + code implementation

class solution {
public:
	vector<int> sort_hash(vector<int>& array){
		vector<int> result;
		int hash_map[1000] = {0};
		for (int i = 0; i < array.size(); i++) {
			hash_map[array[i]]++;
		}
	
		for (int i = 0; i < 1000; i++) {
			for (int j = 0; j < hash_map[i]; j++) {
				result.push_back(i);
			}
		}
		return result;
	}
};

1.3 hash mapping problem

1. Hash mapping of any element


2. Hash mapping conflicts


3. Zipper method to solve conflict

int solution::hash_func(int key, int table_len) {
	return key % table_len;
}
void solution::insert(ListNode* hash_table[], ListNode* node, int table_len) {
	int hash_key = hash_func(node->val, table_len);
	node->next = hash_table[hash_key];
	hash_table[hash_key] = node;
}
bool solution::search(ListNode* hash_table[], int value, int table_len) {
	int hash_key = hash_func(value, table_len);
	ListNode* head = hash_table[hash_key];
	while (head) {
		if (value == head->val) {
			return true;
		}
		head = head->next;
	}
	return false;
}

2. Longest palindrome string

2.1 Title Description

Given a string containing uppercase and lowercase letters, find the longest palindrome string constructed from these letters.

During construction, please pay attention to case sensitivity. For example, "Aa" cannot be regarded as a palindrome string.

Example 1:
Input:
"abccccdd"
Output:
7
Explanation:
The longest palindrome string we can construct is "dccaccd", and its length is 7.

2.2 C + + code implementation

class Solution {
public:
    int longestPalindrome(string s) {
        int array[123]={0};
        int count=0;
        for(int i=0;i<s.size();i++){
            array[s[i]]++;
            if(array[s[i]]%2==0){
                count+=2;
            }
        }
        if(count<s.size()){
            count++;
        }
        return count;
    }
};

3. Word rules

3.1 Title Description

Given a regular pattern and a string str, judge whether str follows the same rule.

The following here refers to perfect matching. For example, there is a corresponding law of two-way connection between each letter in pattern and each non empty word in string str.

Example 1:

input: pattern = "abba", str = "dog cat cat dog"
output: true

Example 2:

input:pattern = "abba", str = "dog cat cat fish"
output: false

Example 3:

input: pattern = "aaaa", str = "dog cat cat dog"
output: false

Example 4:

input: pattern = "abba", str = "dog dog dog dog"
output: false

3.2 algorithm ideas



3.3 C + + code implementation

class Solution {
public:
    bool wordPattern(string pattern, string s) {
        map<string,char> word_map;
        int used[128]={0};
        string word;
        int pos=0;
        s.push_back(' ');

        for(int i=0;i<s.length();i++){
            if(s[i]==' '){
                if(pos==pattern.length()){
                    return false;
                }
                if(word_map.find(word)==word_map.end()){
                    if(used[pattern[pos]]==1){
                        return false;
                    }
                    word_map[word]=pattern[pos];
                    used[pattern[pos]]=1;
                }
                else{
                    if(word_map[word]!=pattern[pos]){
                        return false;
                    }
                }
                pos++;
                word="";
            }
            else{
                word+=s[i];
            }
        }

        if(pos!=pattern.length()){
            return false;
        }
        return true;
    }
};

4. Grouping of letter words

Title Description

Given an array of strings, combine letters and words together. Letter ectopic words refer to strings with the same letters but arranged differently.

Examples:

input: ["eat", "tea", "tan", "ate", "nat", "bat"]
output:
[
  ["ate","eat","tea"],
  ["nat","tan"],
  ["bat"]
]

4.2 algorithm ideas


4.3 C + + code implementation

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        map<string,vector<string>> anagram;
        vector<vector<string>> result;

        for(int i=0;i<strs.size();i++){
            string str=strs[i];
            sort(str.begin(),str.end());

            if(anagram.find(str)==anagram.end()){
                vector<string> item;
                anagram[str]=item;
            }
            anagram[str].push_back(strs[i]);
        }

        map<string,vector<string>>::iterator it;
        for(it=anagram.begin();it!=anagram.end();it++){
            result.push_back((*it).second);
        }
        return result;
    }
};

5. Longest substring without repeated characters

5.1 Title Description

Given a string, please find the length of the longest substring that does not contain duplicate characters.

Example 1:

input: s = "abcabcbb"
output: 3 
explain: Because the longest substring without duplicate characters is "abc",So its length is 3.

Example 2:

input: s = "bbbbb"
output: 1
 explain: Because the longest substring without duplicate characters is "b",So its length is 1.

Example 3:

input: s = "pwwkew"
output: 3
 explain: Because the longest substring without duplicate characters is "wke",So its length is 3.
     Please note that your answer must be the length of the substring,"pwke" It's a subsequence, not a substring.

Example 4:

input: s = ""
output: 0

5.2 algorithm ideas


5.3 C + + code implementation

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int begin=0;
        int result=0;
        string word="";
        int char_map[128]={0};

        for(int i=0;i<s.length();i++){
            char_map[s[i]]++;
            if(char_map[s[i]]==1){
                word+=s[i];
                if(result<word.length()){
                    result=word.length();
                }
            }
            else{
                while(begin<i&&char_map[s[i]]>1){
                    char_map[s[begin]]--;
                    begin++;
                }
                word="";
                for(int j=begin;j<=i;j++){
                    word+=s[j];
                }
            }
        }
        return result;
    }
};

6. Repetitive DNA sequences

6.1 Title Description

All DNA consists of A series of nucleotides abbreviated as' A ',' C ',' G 'and'T', such as "ACGAATTCCG". When studying DNA, identifying repetitive sequences in DNA can sometimes be very helpful.

Write a function to find all target substrings. The length of the target substring is 10 and appears more than once in the DNA string s.

Example 1:

Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
Output:["AAAAACCCCC","CCCCCAAAAA"]

Example 2:

Input: s = "AAAAAAAAAAAAA"
Output:["AAAAAAAAAA"]

6.2 algorithm ideas

6.3 C + + code implementation

class Solution {
public:
    vector<string> findRepeatedDnaSequences(string s) {
        map<string,int> word_map;
        vector<string> result;

        for(int i=0;i<s.length();i++){
            string word=s.substr(i,10);
            if(word_map.find(word)==word_map.end()){
                word_map[word]=1;
            }
            else{
                word_map[word]++;
            }
        }
        map<string,int>::iterator it;
        for(it=word_map.begin();it!=word_map.end();it++){
            if(it->second>1){
                result.push_back(it->first);
            }
        }
        return result;
    }
};

7. Minimum coverage substring

7.1 Title Description

Give you a string s and a string t. Returns the smallest substring in s that covers t all characters. If there is no substring covering t all characters in s, the empty string "" is returned.

Note: if there is such a substring in s, we guarantee that it is the only answer.

Example 1:

Input: s = "ADOBECODEBANC", t = "ABC"
Output:"BANC"

Example 2:

Input: s = "a", t = "a"
Output:"a"

7.2 algorithm ideas


7.3 C + + code implementation

class Solution {
public:
    bool is_window_ok(int map_s[],int map_t[],vector<int>& vec_t){
        for(int i=0;i<vec_t.size();i++){
            if(map_s[vec_t[i]]<map_t[vec_t[i]]){
                return false;
            }
        }
        return true;
    }
    string minWindow(string s, string t) {
        int map_s[128]={0};
        int map_t[128]={0};
        vector<int> vec_t;

        for(int i=0;i<t.length();i++){
            map_t[t[i]]++;
        }         
        for(int i=0;i<128;i++){
            if(map_t[i]>0){
                vec_t.push_back(i);
            }
        }

        int window_begin=0;
        string result;

        for(int i=0;i<s.length();i++){
            map_s[s[i]]++;
            while(window_begin<i){
                char begin_ch=s[window_begin];
                if(map_t[begin_ch]==0){
                    window_begin++;
                }
                else if(map_s[begin_ch]>map_t[begin_ch]){
                    map_s[begin_ch]--;
                    window_begin++;
                }
                else{
                    break;
                }
            }
            if(is_window_ok(map_s,map_t,vec_t)){
                int new_window_len=i-window_begin+1;
                if(result==""||result.length()>new_window_len){
                    result=s.substr(window_begin,new_window_len);
                }
            }
        }
        return result;
    }
};

Topics: data structure