Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Best way to figure out overlapping of strings

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have 2 Strings. Is there a regex way of finding out the overlapping part?


String str1 = "AB-CD";
String str2 = "CH-EF"

Now C is the overlapping part. I did it with tokenizer and with a custom algorithm, going one by one. But Is there a simple way out?

regards
Binu K Idicula
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to another forum since this really more of a question that a Diversion. The Click Here link will take you to its new home.

Eric
 
Marshal
Posts: 74754
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That isn't really an overlap; if you had ABCD and CDEF then the overlap would be CD.

You can try putting one String into a regular expression inside [] and the seeing whether you can match the other. But then you would get ABCD matching FEDC on the DC. In the case you mention you would also have problems with the - because it is a special character.

There is lots and lots of information about regular expressions; the Java Tutorials is a good place to start.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another important question would be what is considered overlap? If you have:

"abcde" and "cdfg" does the "cd" overlap?
"abcde" and "fbcg" does the "bc" overlap?
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
are you asking for "greatest common substring" rather than an "overlap"?
 
Binu K Idicula
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"abcde" and "cdfg" does the "cd" overlap? - YES
"abcde" and "fbcg" does the "bc" overlap? - YES

are you asking for "greatest common substring" rather than an "overlap"? - YES, precisely.

Any thoughts?
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wikipedia has a nice article about this:
http://en.wikipedia.org/wiki/Longest_common_substring_problem
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a link to the Wikipedia article on the subject. It contains a pseudocode algorithm that you might be able to adapt. It's a starting point if nothing else.


edit: 2 minutes too slow!
[ December 03, 2008: Message edited by: Garrett Rowe ]
 
Campbell Ritchie
Marshal
Posts: 74754
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Garrett Rowe:
edit: 2 minutes too slow!



That happens to me all the time whenever Rob Prime is online!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic