We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
If anyone is interested in a recursive solution to the second part :) :
//Get spell and journal namestringspellName=spell->revealScrollName();stringjournal=SpellJournal::journal;// Initialize the memoization table with -1 (indicating uncomputed)intn=spellName.size();intm=journal.size();//memo is used to keep track of the computation, avoiding usless computationvector<vector<int>>memo(n+1,vector<int>(m+1,-1));autof=[&spellName,&journal,&memo](intn,intm,auto&f)->int{if(n==0||m==0)return0;//Reached the end of one stringif(memo[n][m]!=-1)returnmemo[n][m];//If already computedif(spellName[n-1]==journal[m-1]){//Update memo adding the new computationmemo[n][m]=1+f(n-1,m-1,f);}else{memo[n][m]=std::max(f(n-1,m,f),f(n,m-1,f));//Recall f over n-1 and m-1 to gety the substring which return the highest value}returnmemo[n][m];// Return the computed result};
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Magic Spells
You are viewing a single comment's thread. Return to all comments →
If anyone is interested in a recursive solution to the second part :) :