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.
importjava.util.*;importjava.text.*;importjava.math.*;importjava.util.regex.*;publicclassSolution{staticStringmorganAndString(Stringa,Stringb){// Complete this functionintlenA=a.length(),lenB=b.length();StringBuildersb=newStringBuilder();intpA=0,pB=0;while(pA<lenA&&pB<lenB){if(a.charAt(pA)<b.charAt(pB)){sb.append(a.charAt(pA++));}elseif(a.charAt(pA)>b.charAt(pB)){sb.append(b.charAt(pB++));}else{if(compare(a,pA+1,b,pB+1)){sb.append(a.charAt(pA++));while(pA<a.length()&&a.charAt(pA)==a.charAt(pA-1)){sb.append(a.charAt(pA++));}}else{sb.append(b.charAt(pB++));while(pB<b.length()&&b.charAt(pB)==b.charAt(pB-1)){sb.append(b.charAt(pB++));}}}}if(pA<lenA){sb.append(a.substring(pA));}if(pB<lenB){sb.append(b.substring(pB));}returnsb.toString();}privatestaticbooleancompare(Stringa,inti,Stringb,intj){while(i<a.length()&&j<b.length()){if(a.charAt(i)<b.charAt(j))returntrue;elseif(a.charAt(i)>b.charAt(j))returnfalse;i++;j++;}returni==a.length()?false:true;}publicstaticvoidmain(String[]args){Scannerin=newScanner(System.in);intt=in.nextInt();for(inta0=0;a0<t;a0++){Stringa=in.next();Stringb=in.next();Stringresult=morganAndString(a,b);System.out.println(result);}in.close();}}
so use this for good solution
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Morgan and a String
You are viewing a single comment's thread. Return to all comments →
so use this for good solution