string search
if I have a sequence of character like this aaaababbaaa, what would be a good way to find the sequences 'aaa' and print 0 and 'aba' and print 1. Taking into account simplicity and speed of processing. No utility classes to use and no regular expressions.
[265 byte] By [
firoza] at [2007-11-27 11:29:26]

What is the association with the sequences (aaa and aba) and the values?
There is no associations, just when 'aaa' is found 0 is printed and if 'aba' is found 1 is printed.
firoza at 2007-7-29 16:27:53 >

Seems a strange requrement. However, have you bothered to look at the String class in the API?
well if you cant use any utilities:
for(int i = 0; i < string.length(); i++){
sub = string.substring(i, i+3);
if(sub.equals("aaa")){ ...
}
> well if you cant use any utilities:
>
> for(int i = 0; i < string.length(); i++){
>
> sub = string.substring(i, i+3);
>
> if(sub.equals("aaa")){ ...
>
> }
How about if the sequence is too big, is this an efficient way of doing this?
firoza at 2007-7-29 16:27:53 >

Why use a loop. There are two methods of the String class that will provide the required result.
> Why use a loop. There are two methods of the String
> class that will provide the required result.
Are you talking about replace and replaceAll?
firoza at 2007-7-29 16:27:53 >

> > Why use a loop. There are two methods of the
> String
> > class that will provide the required result.
>
> Are you talking about replace and replaceAll?
You should be able to answer that yourself. Are you trying to replace any characters? No. All you are trying to do is see if one string contains another string (big hint).
so what about this, will contains method do that?
The following sequence aababaaabaaa<End Of Input> would produce the following result:100
While the following sequenceaaababaaaabbababa<End Of Input>would produce the following result:0101
So I am not looking at sequence within sequence.
firoza at 2007-7-29 16:27:53 >

> if I have a sequence of character like this
> aaaababbaaa, what would be a good way to find the
> sequences 'aaa' and print 0 and 'aba' and print 1.
> Taking into account simplicity and speed of
> processing. No utility classes to use and no regular
> expressions.
say for example a user enters aaabababaaaaaa
what will be the output?
It would help if you fully explained your friggin requirements in the first place. You claimed that there was no association between the substrings and the output but there clearly is. Isn't there?
If my input is aaaba then the output should be 0. Due to aaa = 0 and you "consume" those letters leaving ba to match against. If this is correct then you will have to use the loop suggest by TuringPest.
> > if I have a sequence of character like this
> > aaaababbaaa, what would be a good way to find the
> > sequences 'aaa' and print 0 and 'aba' and print 1.
> > Taking into account simplicity and speed of
> > processing. No utility classes to use and no
> regular
> > expressions.
>
> say for example a user enters aaabababaaaaaa
> what will be the output?
it would be 0100. for each 'aaa' print 0 and for each 'aba' print 1.
firoza at 2007-7-29 16:27:53 >

> > > if I have a sequence of character like this
> > > aaaababbaaa, what would be a good way to find
> the
> > > sequences 'aaa' and print 0 and 'aba' and print
> 1.
> > > Taking into account simplicity and speed of
> > > processing. No utility classes to use and no
> > regular
> > > expressions.
> >
> > say for example a user enters aaabababaaaaaa
> > what will be the output?
>
> it would be 0100. for each 'aaa' print 0 and for each
> 'aba' print 1.
aaabababaaaaaa
aaa=0
bab = ?
aba = 1
aaa = 0
aaa = 0
so what about bab?
> > > > if I have a sequence of character like this
> > > > aaaababbaaa, what would be a good way to find
> > the
> > > > sequences 'aaa' and print 0 and 'aba' and
> print
> > 1.
> > > > Taking into account simplicity and speed of
> > > > processing. No utility classes to use and no
> > > regular
> > > > expressions.
> > >
> > > say for example a user enters aaabababaaaaaa
> > > what will be the output?
> >
> > it would be 0100. for each 'aaa' print 0 and for
> each
> > 'aba' print 1.
> aaabababaaaaaa
> aaa=0
> bab = ?
> aba = 1
> aaa = 0
> aaa = 0
>
> so what about bab?
bab will be ignored.
firoza at 2007-7-29 16:27:53 >

>for(int i = 0; i < string.length(); i++){
>sub = string.substring(i, i+3);
>if(sub.equals("aaa")){ ...
>}
would this not produce an out of bounds-ish exception?
you would obviously alter the code to avoid that, lol.
> would this not produce an out of bounds-ish exception?
Ssssssssssh!
The OP has to figure something out for themselves.
public class SimpleEx {
public static void main(String[] args){
String s = "aaabababaaaaaa";
for(int i=0; i<s.length(); i++) {
s = s.replaceFirst("aaa", "0");
s = s.replaceAll("aba", "1");
if(s.contains("b"))
s = s.replaceAll("b", "");
}
System.out.println(s);
}
}
>
public class FindString {
public static void main(String args[]){
String str="aaaababbaaa";
if (str.indexOf("aaa") >= 0)
System.out.println("0");
if (str.indexOf("aba") >= 0)
System.out.println("1");
}
}
What is it with all these fucking idiots posting code. All you are doing is helping them to cheat. They do not learn anything. If you help them to cheat their way to graduating, you will probably have one of them as you boss one day. Do you still want to do all their work while they get paid twice as much as you?
DO NOT POST CODE!
if they become my boss because they found "aaa" in "aabaabaa"
it will probably be raining blood and i will be hiding under my desk
from one of the horseman of the apocalypse.
take a deep breath, lol. ive never seen anyone write any "real" code.
its always this 100 level bullsh|t.