To understand this, we must first understand what search is.
Search is familiar to everyone, DFS, BFS, depth-first search, and breadth-first search.
What is memory search? It is in this recursion search process, for some repeated sub-problems, find a data structure to record the results of the last calculation.
Does it sound a bit like Dynamic Programming?
Yes, the memory search is recursion, from top to bottom, while Dynamic Programming extracts the recursion process into state transition equations, memorizes the data structure into DP arrays, and then calculates from bottom to top.
if (!checkIfSimilar(i1, i2, length, s1, s2)) { returnfalse; }
for (let i = 1; i < length; i++) { if (dfs(i1, i2, i, s1, s2) && dfs(i1 + i, i2 + i, length - i, s1, s2)) { returntrue } if (dfs(i1, i2 + length - i, i, s1, s2) && dfs(i1 + i, i2, length - i, s1, s2)) { returntrue; } }
for (let i = 1; i < length; ++i) { if (dfs(i1, i2, i, s1, s2, memo) && dfs(i1 + i, i2 + i, length - i, s1, s2, memo)) { memo[i1][i2][length] = 1; returntrue; } if (dfs(i1, i2 + length - i, i, s1, s2, memo) && dfs(i1 + i, i2, length - i, s1, s2, memo)) { memo[i1][i2][length] = 1; returntrue; } }
memo[i1][i2][length] = -1; returnfalse; }
const checkIfSimilar = function(i1, i2, length, s1, s2) { const freq = newMap(); for (let i = i1; i < i1 + length; ++i) { const c = s1[i]; freq.set(c, (freq.get(c) || 0) + 1); } for (let i = i2; i < i2 + length; ++i) { const c = s2[i]; freq.set(c, (freq.get(c) || 0) - 1); } for (const value of freq.values()) { if (value ! 0) { returnfalse; } } returntrue; }
Memory Search and Dynamic Programming
It should be said that the memory search algorithm is top-down, and the Dynamic Programming algorithm is bottom-up. Both are implementations of the idea of Dynamic Programming algorithm.
In theory, Dynamic Programming algorithm is a further step of some memory search, that is to say, in theory, all DP algorithms can be written as memory search.