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.
My logic is taking an char from s1 and find it in s2 and if found remove the part of s2 before that particular character and increment counter and repeat for next character from s1.
Above iteration is done by taking character from s2 and finding them in s1.
finnaly maximum of two methods is the answer
Can anyone tell the mistake in my logic?
importjava.io.*;importjava.util.*;importjava.text.*;importjava.math.*;importjava.util.regex.*;publicclassSolution{staticintcommonChild1(Strings1,Strings2){intcount=0;intnowfrom=0;Stringtemp=s2;for(inti=0;i<s1.length();i++){Stringfinds=""+s1.charAt(i);//taking char from s1 if(temp.contains(finds))//if tempcontians character than cut the part of string before that char in temp and increment count{nowfrom=temp.indexOf(finds)+1;count++;}if(nowfrom>=temp.length())break;temp=temp.substring(nowfrom,temp.length());}returncount;}staticintcommonChild2(Strings1,Strings2){intarray1[]=newint[26];intarray2[]=newint[26];intcount=0;intnowfrom=0;Stringtemp=s1;for(inti=0;i<s2.length();i++){Stringfinds=""+s2.charAt(i);if(temp.contains(finds)){nowfrom=temp.indexOf(finds)+1;count++;}if(nowfrom>=temp.length())break;temp=temp.substring(nowfrom,temp.length());}returncount;}publicstaticvoidmain(String[]args){Scannerin=newScanner(System.in);Strings1=in.next();Strings2=in.next();intresult=Math.max(commonChild1(s1,s2),commonChild2(s1,s2));System.out.println(result);}}
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 →
My logic is taking an char from s1 and find it in s2 and if found remove the part of s2 before that particular character and increment counter and repeat for next character from s1.
Above iteration is done by taking character from s2 and finding them in s1.
finnaly maximum of two methods is the answer
Can anyone tell the mistake in my logic?