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.
publicstaticintsteadyGene(Stringgene){// Write your code hereintsuffi=0;intans=500000;intlimit=gene.length()/4;LinkedList<Result>stack=newLinkedList<>();Map<Character,Integer>map=newHashMap<>(4);map.put('A',0);map.put('T',0);map.put('G',0);map.put('C',0);for(inti=0;i<gene.length();i++){map.put(gene.charAt(i),map.get(gene.charAt(i))+1);}Stringpatt="AGTC";for(inti=0;i<patt.length();i++){intnum=map.get(patt.charAt(i));if(num<=limit){map.put(patt.charAt(i),-1);}else{suffi+=(map.get(patt.charAt(i))-limit);}}for(inti=0;i<gene.length();i++){charcurr=gene.charAt(i);if(map.get(curr)!=-1){if(map.get(curr)>limit){suffi--;}stack.add(newResult(i,curr));map.put(curr,map.get(curr)-1);while(true){if(map.get(stack.getFirst().element)<limit){map.put(stack.getFirst().element,map.get(stack.getFirst().element)+1);stack.removeFirst();}else{break;}}if(suffi<=0){ans=Math.min(ans,stack.getLast().idx-stack.getFirst().idx+1);}}}returnans;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Bear and Steady Gene
You are viewing a single comment's thread. Return to all comments →