
string cut = "ahlllaaaa"; i want to get p(a) = 5/9 p(h) = 1/9 p(l) = 3/9
how can i do that ??????????????????????

Hello Friend,
Try the following code:
class Probability{
static int count(char c, String str) {
if (str == null) return 0;
int cnt = 0;
for (int i = 0;; cnt++) {
if ((i = str.indexOf(c,i)+1) == 0) break;
}
return cnt;
}
public static void main(String[] args){
String cut = "ahlllaaaa";
String s = cut.replaceAll((String.format("(.)(?<=(?:(?=\\1).).{0,%d}(?:(?=\\1).))", cut.length())), "");
String st=s.replace(" ","");
int len=cut.length();
char character[]=st.toCharArray();
char ch=' ';
for(int i=0;i<character.length;i++){
ch=character[i];
int c= count(ch,cut);
System.out.println("p("+ch+")="+c+"/"+len);
}
}
}
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.