TheAlgorithms / Solidity

Algorithms and data structures implemented in Solidity
GNU Lesser General Public License v2.1
332 stars 95 forks source link

LCS contraction addition in dynamic programming #81

Open priyda opened 1 year ago

priyda commented 1 year ago

The contract has two functions:

The lcs function takes two strings and the lengths of the strings as input and returns the length of the LCS of the strings. The view keyword specifies that this function does not modify the state of the contract, so it does not need to be executed on the blockchain.

The max function is a helper function that takes two unsigned integers and returns the maximum of the two. The private keyword specifies that this function can only be called from within the contract.

The lcs function first checks if the result for the input strings is already stored in the cache mapping. If it is, the function returns the cached result. If the result is not in the cache, the function calculates the result using the standard recursive definition of the LCS problem and stores the result in the cache before returning it.