• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generate all possible substrings

 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey everyone,

This is an assignment that I had last semester when I took my advanced programming class, but I was unable to figure it out. I stumbled across it this morning and was wondering if someone can give me some input on what's going wrong.

We were supposed to used a recursive method to generate all possible substrings of a given string without repeats.

Here's the code:




If I pass in cat into my program I get:
cat
ca
c
a
at
a
t

I'm not really sure where the repeated a is coming from.

Thanks in advance,
Hunter.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hunter McMillen wrote:Hey everyone,

This is an assignment that I had last semester when I took my advanced programming class, but I was unable to figure it out. I stumbled across it this morning and was wondering if someone can give me some input on what's going wrong.


If I pass in cat into my program I get:
cat
ca
c
a
at
a
t

I'm not really sure where the repeated a is coming from.

Thanks in advance,
Hunter.



I did not really look at the code, but it looks like the substrings "ca" and "at" are extracted from cat.

Then "ca" is further divided into "c" and "a" as well as "at" is further divided into "a" and "t".

 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
not printing duplicates could get hard. imagine if your input string is "the fat cat sat on a rat". it's easy to see how you might get "at" a lot...

What I'd probably do is generate each one like you are, but instead of printing it, insert it into a set.

Then, after you are all done generating them, iterate over your set elements to print them out.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help guys, i figured it out.


This is what I changed it to:





Hunter.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Well done



Seconded!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic