Skip to content

Instantly share code, notes, and snippets.

@chaoxu
Created September 26, 2010 15:04
Show Gist options
  • Save chaoxu/597993 to your computer and use it in GitHub Desktop.
Save chaoxu/597993 to your computer and use it in GitHub Desktop.
import java.util.*;
public class bio{
public static void main(String[] args){
char[] c = "aabc".toCharArray();
//String s = "eabahgcabah";
String s = "aaaaaaabc";
int[] r = list(c,s);
for(int i=0;i<r.length;i++){
System.out.println(r[i]);
}
}
public static int[] list(char[] c1, String s1){
//Preparing the data
char[] t = s1.toCharArray();
int[] s = new int[t.length];
int[] c = new int[c1.length];
for(int i=0;i<s.length;i++){
s[i] = (int) t[i];
}
for(int i=0;i<c.length;i++){
c[i] = (int) c1[i];
}
int[] z = new int[256];
for(int i=0;i<c.length;i++){
z[c[i]]++;
}
for(int i=0;i<256;i++){
if(z[i]==0){
z[i] = -1;
}
}
Queue<Integer> q = new LinkedList<Integer>();
ArrayList<Integer> l = new ArrayList<Integer>();
//The algorithm
for(int i=0;i<s.length;i++){
if(z[s[i]]<0){
//pop the entire queue;
while(q.size()!=0){
z[q.remove()]++;
}
}else if(z[s[i]]==0){
int o = q.remove();
while(o!=s[i]){
z[o]++;
o = q.remove();
}
q.offer(s[i]);
}else{
z[s[i]]--;
q.offer(s[i]);
}
if(q.size()==c.length){
l.add(i+2-c.length);
}
//System.out.println(q);
}
int[] r = new int[l.size()];
for(int i=0;i<l.size();i++){
r[i] = l.get(i);
}
return r;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment