Skip to content

Fun Algorithm

Published: at 09:38 AM

Detecting Single-Character Differences

Here’s an algorithm to check if the input array is a sequence of strings that differs by exactly one character.

Breaking Down the Algorithm:

bool HasExactlyOneCharacterDifference(string A, string B){
    var singleMismatch = false;

    for(int i=0; i<A.Length; i++){
        if(A[i] != B[i]){
            if(singleMismatch) return false;
            singleMismatch = true;
        }
    }

    return singleMismatch;
}

bool FindSequenceWithSingleCharacterChanges(string next, List<string> list){
    foreach(var item in list){
        if(HasExactlyOneCharacterDifference(next, item)){
            var listCopy = new List<string>(list);
            Console.WriteLine($"Matched: {next} to {item}");
            if(listCopy.Count == 2) return true;
            Console.WriteLine(listCopy.Count);
            listCopy.Remove(next);
            if(FindSequenceWithSingleCharacterChanges(item, listCopy)){
                return true;
            }
        }
    }
    return false;
}

bool HasValidSequence(string[] inputArray) {
    foreach(var item in inputArray){
        if(FindSequenceWithSingleCharacterChanges(item, inputArray.ToList())){
            return true;
        }
    }
    return false;
}

The core of the algorithm works in three parts:

  1. HasExactlyOneCharacterDifference Function:

    • Compares two strings character by character.
    • Returns true if there’s exactly one mismatched character, false otherwise.
  2. FindSequenceWithSingleCharacterChanges Function:

    • Recursively explores possible chains of strings.
    • Takes a next string and a list of remaining strings as input.
    • For each remaining string, checks if it has a single mismatch with next.
    • If a match is found:
      • Prints the matched pair.
      • Creates a copy of the remaining list, removing the matched string.
      • Recursively calls FindSequenceWithSingleCharacterChanges with the matched string and the reduced list.
      • If the recursive call returns true, the entire chain is valid.
      • If the recursive call returns false, the function backtracks and explores other possibilities.
  3. HasValidSequence Function:

    • Iterates through each string in the input array.
    • Calls the FindSequenceWithSingleCharacterChanges function with the current string and the remaining list.
    • If any chain is found, the function returns true.
    • If no chain is found after checking all strings, the function returns false.

Example: Let’s take an example with input:

string[] inputArray = { "abc", "abx", "abz", "xyz", "zyx" };
bool result = HasValidSequence(inputArray);
Console.WriteLine($"Result: {result}");

If there are strings in the list that only have one mismatch with each other, the algorithm will print the matched pairs and return true when a valid match is found.

Key Points:


Previous Post
See Clearly, Live Vibrantly - A Guide to Eye Care in the Digital Age
Next Post
Mrs. Dalloway - Minimal, Optimised Reading Experience.