You are viewing a single comment's thread. Return to all comments →
java :
public static int steadyGene(String s) { // Write your code here int n=s.length();
Map<Character,Integer> map=new HashMap<>(); for(char c:s.toCharArray()){ map.put(c,map.getOrDefault(c,0)+1); } int a[]=new int[256]; int len=0; for(char key:map.keySet()){ if(map.get(key)<=n/4){ continue; } else{ a[key]=map.get(key)-(n/4); System.out.println(key+" "+a[key]); len+=map.get(key)-(n/4); } } if(len==0){ return 0; } int c=0; int start=0; int ans=Integer.MAX_VALUE; int b[]=new int[256]; for(int i=0;i<n;i++){ b[s.charAt(i)]++; if(b[s.charAt(i)]<=a[s.charAt(i)]){ c++; } if(c==len){ System.out.println(c); while(start<n&&(b[s.charAt(start)]>a[s.charAt(start)]) || a[s.charAt(start)]==0){ if(b[s.charAt(start)]>a[s.charAt(start)]) { b[s.charAt(start)]--; } start++; } if(i-start+1<ans){ ans=i-start+1; } } } return ans; }
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 →
java :
public static int steadyGene(String s) { // Write your code here int n=s.length();