Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Count unique substrings in a string

 
Ranch Hand
Posts: 87
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Problem link: Distint Substrings

I tried solving using prefix function based on Prefix function - KMP

but the solution described is O(n^2). And I want to solve it in O(n)


 
Marshal
Posts: 79632
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using StringTokenizer, and InputStreamReader, which are regarded as legacy code?

I think you may get more attention if I add you to one of our other fora.
 
Bhaskar Bantupalli
Ranch Hand
Posts: 87
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why are you using StringTokenizer, and InputStreamReader, which are regarded as legacy code?


I have been using a template for online problem solving. I haven't changed it in years. Which API do you recommend that works for java 8 and 8+ alike

I think you may get more attention if I add you to one of our other fora.

Please add me. Can you say what the other forum is?

 
Campbell Ritchie
Marshal
Posts: 79632
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you envisage an algorithm running in linear time?

Bhaskar Bantupalli wrote:. . . . Which API do you recommend that works for java 8 and 8+ alike

I would use a Scanner to read System.in. It does its own tokenising which makes it unnecessary to use the tokeniser. It also provides methods like nextInt() which make it unnecessary to use Integer.parseInt() or similar.
For other input, use a Path object, as described in the Java™ Tutorials.

. . . Can you say what the other forum is?

Java in General.
 
Bartender
Posts: 5526
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Bhaskar,

have you read or heard somewhere that an O(N) solution is possible? The ones I found on the internet are all O(N^2).
 
Bhaskar Bantupalli
Ranch Hand
Posts: 87
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

have you read or heard somewhere that an O(N) solution is possible?

- yes
Based on the time limits and input limits on the problem, the problem if solved cannot be O(n^2). and based on below solutions which I didn't understand much, I thought it is O(n)
https://github.com/Jonathan-Uy/CSES-Solutions/blob/main/String%20Algorithms/Distinct%20Substrings.cpp
https://codeforces.com/blog/entry/95004 - 12th solution and code

 
Master Rancher
Posts: 4966
78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it looks like there is an O(N) solution to this problem using Suffix Automaton.  I'm not sure if there are any other algorithms that solve the problem as efficiently.
 
Piet Souris
Bartender
Posts: 5526
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now, that is a great site! Bookmarked it.
 
Mike Simmons
Master Rancher
Posts: 4966
78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agreed - I hadn't seen it before, but found it in Bhaskar's first post above.  When another post mentioned suffix automaton, I searched in cp-algorithms.com because that was the resource Bhaskar was using... and there it was.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic