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.
ISITENOUGHFAST?publicstaticintcommonChild(Strings1,Strings2){// Write your code hereMap<Integer,ArrayList<Integer>>map=newHashMap<>(s1.length());Map<Integer,int[]>res=newHashMap<>(s1.length());booleanld=false;int[]a=newint[2];a[0]=0;a[1]=-1;res.put(1,a);intmaxNum=1;for(inti=0;i<s1.length();i++){if(map.containsKey((int)s1.charAt(i))==false){map.put((int)s1.charAt(i),newArrayList<>());}map.get((int)s1.charAt(i)).add(i);}for(inti=0;i<s2.length();i++){if(map.containsKey((int)s2.charAt(i))==true){for(intj=maxNum;j>0;j--){intval=res.get(j)[1];intpre=res.get(j)[0];for(inttmp:map.get((int)s2.charAt(i))){if(tmp>val){int[]b=newint[2];b[0]=val;b[1]=tmp;if(ld==false){ld=true;res.put(maxNum,b);}else{if(j==maxNum){res.put(j+1,b);maxNum++;}else{if(res.get(j+1)[1]>tmp){res.put(j+1,b);}}}break;}else{if(tmp>pre&&tmp<res.get(j)[1]){res.get(j)[1]=tmp;}}}}}}if(ld==false)maxNum--;returnmaxNum;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Common Child
You are viewing a single comment's thread. Return to all comments →