Daily algorithm & interview questions, 28 days of special training in large factories - the 15th day (string)

Posted by dey.souvik007 on Mon, 21 Feb 2022 01:31:38 +0100


This article comes from the special training column of algorithm interview questions. There are a large number of professional algorithm questions here, such as 21 days of dynamic planning, 28 days of special training in large factories, etc
Welcome to study together.

Link: Portal

Reading guide

Fat friends, in order to better help new students adapt to algorithms and interview questions, we recently started a special assault step by step. In the last issue, we completed 21 days of dynamic programming, and now we will make a 28 day summary of various algorithms. What are you waiting for? Come and have a 28 day challenge with Feixue!!

Special introduction

📣 Xiaobai training column is suitable for newcomers who have just started or students who take the second level of python. You are welcome to subscribe Programming Xiaobai advanced

📣 The interesting Python hand training project includes interesting articles such as robot awkward chat and spoof program, which can make you happy to learn python Training project column

📣 In addition, students who want to learn java web can take a look at this column: Teleporters

📣 This is a sprint factory interview column and algorithm competition practice. Let's cheer together The way ashore

Algorithm training for 28 days

Please judge whether a 9 x 9 Sudoku is effective. You only need to verify whether the numbers you have filled in are valid according to the following rules.

The numbers 1-9 can only appear once in each line. The numbers 1-9 can only appear once in each column. The numbers 1-9 are in each 3x3 separated by a thick solid line
Intrauterine can only occur once. (please refer to the example diagram)

be careful:

An effective Sudoku (partially filled) is not necessarily solvable. You only need to verify whether the filled number is correct according to the above rules
The effect is enough. Use '.' for blank space express. Give you two strings: ransomNote and magazine. Judge whether ransomNote can be
The characters in magazine.

If yes, return true; Otherwise, false is returned.

Each character in magazine can only be used once in ransomNote.

Example 1:

Input: ransomNote = "a", magazine = "b"
Output: false
Example 2:

Input: ransomNote = "aa", magazine = "ab"
Output: false
Example 3:

Input: ransomNote = "aa", magazine = "aab"
Output: true

Idea: the title requires the characters in the string \ textit{magazine}magazine to build a new string
\textit{ransomNote}ransomNote, and \ textit{ransomNote}ransomNote
Each character in the string can only be used once, which only needs to meet each English letter in the string \ textit{magazine}magazine
(\ texttt {a '-' z '}) (' a '-' z ') the number of statistics is greater than or equal to \ textit{ransomNote}ransomNote
You can count the number of times of the same letter in.

class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        if (ransomNote.length() > magazine.length()) {
            return false;
        }
        int[] cnt = new int[26];
        for (char c : magazine.toCharArray()) {
            cnt[c - 'a']++;
        }
        for (char c : ransomNote.toCharArray()) {
            cnt[c - 'a']--;
            if(cnt[c - 'a'] < 0) {
                return false;
            }
        }
        return true;
    }
}


Given two strings s and t, write a function to judge whether t is an alphabetic ectopic word of s.

Note: if each character in S and t appears the same number of times, s and T are called alphabetic words.

Example 1:

input: s = "anagram", t = "nagaram"
output: true
Example 2:

input: s = "rat", t = "car"
output: false

Idea: t is the ectopic word of ss, which is equivalent to "two strings are equal after sorting". So we can compare the strings ss and tt
Sort them separately, and judge whether the sorted strings are equal. In addition, if the length of ss and tt is different, tt must not be an ectopic word of ss.

class Solution {
    public boolean isAnagram(String s, String t) {
        if (s.length() != t.length()) {
            return false;
        }
        char[] str1 = s.toCharArray();
        char[] str2 = t.toCharArray();
        Arrays.sort(str1);
        Arrays.sort(str2);
        return Arrays.equals(str1, str2);
    }
}


Interview questions

Redis The difference between data structure compressed list and jump table
 Compressed list( ziplist)It is essentially an array of bytes Redis A linear controller designed to save memory
 Data structure can contain multiple elements, and each element can be a byte array or an integer.
30
 Jump table( skiplist)Is an ordered data structure, which maintains multiple fingers pointing to other nodes in each node
 Needle, so as to achieve the purpose of fast access to nodes. Jump table supports average O(logN),worst O(N)Complexity
 You can also batch process nodes through sequential operations


Redis How is master-slave synchronization realized?
Full synchronization
master The server will start a background process for redis Generate a rdb Documents, at the same time,
The server will cache all received write commands (including add, delete and change) from the client, and when the background saving process is finished
 After completion, the rdb File transfer to slave Server, and slave The server will rdb Save the file on disk and
 Load the data into memory by reading the file, and then master The server will cache the
 Command passed redis Transmission protocol sent to slave Server, and then slave The server acts on these commands in turn
 Finally achieve data consistency on their own local data sets.


Incremental synchronization
 from redis 2.8 Before the version, partial synchronization was not supported. When the connection between the master and slave servers was broken, master clothes
 Server and slave Full data synchronization is carried out between servers.
from redis 2.8 In the beginning, even if the master-slave connection is broken halfway, full synchronization is not required, because from this version
 The concept of partial synchronization is integrated. The implementation of partial synchronization depends on master Server memory for each slave service
 The synchronizer maintains a synchronization log and synchronization ID for each slave The server is following master When the server synchronizes
 Carry your own synchronization ID and the last location of the last synchronization. When the master-slave connection is broken, slave Server partition time
(Default 1 s)Active attempt and master Connect to the server if the offset ID carried from the server is still there
master From the synchronous backup log on the server slave The offset sent starts to continue the last synchronization operation
 Do, if slave The offset sent is no longer master In the synchronous backup log of (possibly due to the disconnection between master and slave)
For a long time or in a short time master If the server receives a large number of writes, it must
 One full update. During partial synchronization, master The instructions recorded in the locally recorded synchronous backup log will be recorded in sequence
 Send to slave Server to achieve data consistency.


Redis Master slave synchronization strategy
 When the master and slave are just connected, full synchronization is carried out; After full synchronization, perform incremental synchronization. Of course, if necessary,
slave Full synchronization can be initiated at any time. redis The strategy is to try incremental synchronization first anyway,
If unsuccessful, the slave is required to perform full synchronization.

Click to get the data directly

There are python, Java learning materials, interesting programming projects and various resources that are hard to find. It's not a loss to look at it anyway.

Topics: Java Algorithm