Looking for an efficient algorithm to find the longest substring in a string that has its complementary string as well (bitwise).
That what I mean by saying complementary string bitwise:
100011
011100
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Here’s a simple O(n) algorithm that relies on suffix tree construction.
Load the original string s and the complement string s’ into the same suffix tree (O(n) time). Then post-process this tree by setting for each node x two flags f(x) and f'(x) that are true exactly when f(x) (resp. f'(x)) contains a suffix of s (resp. s’). Now simply traverse the tree looking for the deepest node that has both flags set and you have found the longest string in s whose complement also occurs in s. The post-processing also costs only O(n) time so the total running time is O(n).