0

I am following a book and I came across a question : Can you spot a pattern in this two pieces of strings?

  1. 112a4a342cb214d0001acd24a3a12dadbcb4a0000000
  2. 1b2a34d4ac42d23b141acd24a3a12dadbcb4a2134141

The thing is I know that 'acd24a3a12dadbcb4a' is the matching piece. However, What method can I use if I don't already know that 'acd24a3a12dadbcb4a' in both of the strings.

Everytime I search for something like that typing pattern I get (REGEX) but that would only work if I give it a regex, The thing is I don't know the regex the program should detect it. I hope you can help me

Everytime I search for something like that typing pattern I get (REGEX) but that would only work if I give it a regex, The thing is I don't know the regex the program should detect it.

  • 1
    When it comes to algorithms, I doubt you can invent something, but rather just use [invented algorithms](https://en.wikipedia.org/wiki/Longest_common_substring_problem) – Alexey S. Larionov Nov 03 '22 at 15:01
  • 1
    Have a look at this https://stackoverflow.com/questions/18715688/find-common-substring-between-two-strings – robinood Nov 03 '22 at 15:02
  • You might want to look into the Longest Common Substring problem – practical-plankton Nov 03 '22 at 15:04
  • Thank you guys This helped, Can this be applied on more than two strings? like a list of some strings? – Esmael Awad Nov 03 '22 at 15:10
  • Does this answer your question? [Find common substring between two strings](https://stackoverflow.com/questions/18715688/find-common-substring-between-two-strings) – wovano Nov 09 '22 at 11:50

1 Answers1

0

Your length of your both string is 44 characters. In both string 1. and 2. common pattern starts from index 18 and ends at index 37.

So here's the example through which you match the similar pattern in all strings.

str1 = "112a4a342cb214d0001acd24a3a12dadbcb4a0000000"
str2 = "1b2a34d4ac42d23b141acd24a3a12dadbcb4a2134141"

common_string = str1[18:37]
if(common_string in str2):
    # write you logic over here.
Nav
  • 61
  • 6