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]
# 1

What is the association with the sequences (aaa and aba) and the values?

floundera at 2007-7-29 16:27:53 > top of Java-index,Java Essentials,New To Java...
# 2

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 > top of Java-index,Java Essentials,New To Java...
# 3

Seems a strange requrement. However, have you bothered to look at the String class in the API?

floundera at 2007-7-29 16:27:53 > top of Java-index,Java Essentials,New To Java...
# 4

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")){ ...

}

TuringPesta at 2007-7-29 16:27:53 > top of Java-index,Java Essentials,New To Java...
# 5

> 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 > top of Java-index,Java Essentials,New To Java...
# 6

Why use a loop. There are two methods of the String class that will provide the required result.

floundera at 2007-7-29 16:27:53 > top of Java-index,Java Essentials,New To Java...
# 7

> 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 > top of Java-index,Java Essentials,New To Java...
# 8

> > 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).

floundera at 2007-7-29 16:27:53 > top of Java-index,Java Essentials,New To Java...
# 9

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 > top of Java-index,Java Essentials,New To Java...
# 10

> 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?

lrngjavaa at 2007-7-29 16:27:53 > top of Java-index,Java Essentials,New To Java...
# 11

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.

floundera at 2007-7-29 16:27:53 > top of Java-index,Java Essentials,New To Java...
# 12

> > 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 > top of Java-index,Java Essentials,New To Java...
# 13

> > > 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?

lrngjavaa at 2007-7-29 16:27:53 > top of Java-index,Java Essentials,New To Java...
# 14

> > > > 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 > top of Java-index,Java Essentials,New To Java...
# 15

>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?

lance_dragonsa at 2007-7-29 16:27:57 > top of Java-index,Java Essentials,New To Java...
# 16

you would obviously alter the code to avoid that, lol.

TuringPesta at 2007-7-29 16:27:57 > top of Java-index,Java Essentials,New To Java...
# 17

> would this not produce an out of bounds-ish exception?

Ssssssssssh!

The OP has to figure something out for themselves.

floundera at 2007-7-29 16:27:57 > top of Java-index,Java Essentials,New To Java...
# 18

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);

}

}

>

lrngjavaa at 2007-7-29 16:27:57 > top of Java-index,Java Essentials,New To Java...
# 19

That won't work either!

floundera at 2007-7-29 16:27:57 > top of Java-index,Java Essentials,New To Java...
# 20

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");

}

}

jeyrama at 2007-7-29 16:27:57 > top of Java-index,Java Essentials,New To Java...
# 21

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!

floundera at 2007-7-29 16:27:57 > top of Java-index,Java Essentials,New To Java...
# 22

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.

TuringPesta at 2007-7-29 16:27:57 > top of Java-index,Java Essentials,New To Java...
# 23

SOLUTION: Outsource!

:D

kraton_mayaa at 2007-7-29 16:27:57 > top of Java-index,Java Essentials,New To Java...