4

I have to implement an algorithm which takes as input two strings, and returns an array containing substring ranges of changes.

Say, a range is defined as

typedef struct _NSRange {
    NSUInteger location; // Where the affected substring begins
    NSUInteger length; // How long the affected substring is
} NSRange;

Example:

string1 = "My cat sometimes likes to eat fish.";
string 2 = "My cat always likes to drink fresh water, and eat fish.";

The changes are:

  • {7,9} "sometimes" changed to {7,6} "always"
  • {26,0} added "drink fresh water, and "

I need an array which contains substrings grouped by changes. In this example it would look like this:

  • "My cat "
  • "always"
  • " likes to "
  • "drink fresh water, and"
  • " eat fish."

The goal is to highlight those changes in an existing string, for which I must split that string into substrings based on changes.

Before reinventing the wheel - is there a solution in the public domain?

Proud Member
  • 40,078
  • 47
  • 146
  • 231

2 Answers2

2

We split the task into two parts.

Part 1 : Finding the differences.

You can do this using the following code,

NSString *string1 = @"My cat sometimes likes to eat fish.";
 NSString * string2 = @"My cat always likes to drink fresh water, and eat fish.";
NSMutableSet *set1 = [NSMutableSet setWithArray:[string1 componentsSeparatedByString:@" "]];
NSMutableSet *set2 = [NSMutableSet setWithArray:[string2 componentsSeparatedByString:@" "]];
[set2 minusSet:set1];
NSLog(@"%@",set2);

Part 2: Highlighting the words.

Once after knowing the words it is easy to highlight.

Vignesh
  • 10,205
  • 2
  • 35
  • 73
  • Interesting approach - but how would you handle this: "My water cat likes water."; "My water cat likes fish." - Two occurences of water. – Proud Member Feb 29 '12 at 17:43
  • Updated example: "My water cat likes water with salt."; "My water cat likes fish with water and sugar." - the highlights would appear wrong if applied from left to right based on matches ("fish", "and", "sugar.") – Proud Member Feb 29 '12 at 17:55
  • @MikhaloIvanokov. Ya you are correct. It fails in that case. We can make quick hack to solve it. let me update my code. – Vignesh Mar 02 '12 at 04:31
0

You are basically trying to implement the equivalent of diff. As explained in the wikipedia page, it uses the longest common subsequence problem algorithm.

sch
  • 27,436
  • 3
  • 68
  • 83
  • 1
    While this may technically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) for you to include the essential parts of the linked article in your answer, and provide the link for reference. – jscs Feb 29 '12 at 19:17
  • I appreciate your remark. The OP was asking if there is an algorithm that does what he is trying to achieve. So I confirmed that and gave him the name of the algorithm. That was my answer. The linked article here is the one about diff, and the essential parts are the name of the algorithm, not its details. – sch Feb 29 '12 at 19:30