I have been trying to implement an efficient string comparing algorithm that will given points according to character changes.
For example:
String #1: abcd
String #2: acdb
Initial Point: 0
In here String #2 character c changed it’s index from 2 to 1, and d changed its index from 4 to 3. Both of them (2-1=1 and 4-3=1) adds up to 2 points to initial point. Not a homework or anything, I just didn’t want to create a basic for loop comparing each character one by one and wanted to ask if anything efficient method (like hashing etc.) can be applied?
You are overcomplicating a simple thing. You can’t get more efficient than comparing each character and stopping the comparison at the first character you find different – which is basically what
strcmpdoes. The only typical optimization you can do is, if you already know the length of the two strings (as happens when you usestd::stringor other counted strings), to determine them unequal immediately if the two length differ.