Standard Finite-State-Machine (FSM) Implementation - II

There are multiple ways to solve this problem, but I find a state machine implementation very intuitive, even if we have to leave some of the state transitions empty (no-op). The state manages whether you're in "same char" mode or "different char". Code is down below, cheers, ACC.




You are given two strings s1 and s2 of the same length.

A mismatched segment is a maximal contiguous range of indices where the characters of s1 and s2 differ at every index.

Return a 2D array containing [sub1, sub2] for each mismatched segment, where sub1 and sub2 are the corresponding substrings from s1 and s2, respectively. Return the segments in increasing order of their starting indices. If there are no mismatched segments, return an empty array.

A substring is a contiguous non-empty sequence of characters within a string.

 

Example 1:

Input: s1 = "earth", s2 = "empty"

Output: [["ar","mp"],["h","y"]]

Explanation:

  • The characters at indices 1 and 2 differ, forming the mismatched segment ["ar","mp"].
  • The characters at index 4 differ, forming the mismatched segment ["h","y"].

Example 2:

Input: s1 = "action", s2 = "nation"

Output: [["ac","na"]]

Explanation:

The characters at indices 0 and 1 differ, forming the mismatched segment ["ac","na"]. All remaining characters are equal in both strings.

Example 3:

Input: s1 = "hot", s2 = "hot"

Output: []

Explanation:

The two strings are identical, so there are no mismatched segments.

 

Constraints:

  • 1 <= s1.length == s2.length <= 100
  • s1 and s2 consist of lowercase English letters.

public class Solution {
    public IList> FindMismatchedSegments(string s1, string s2)
    {
        bool stateMatches = false;

        List> retVal = new List>();
        string mismatchS1 = "";
        string mismatchS2 = "";

        for (int i = 0; i < s1.Length; i++)
        {
            if (s1[i] == s2[i])
            {
                if (stateMatches)
                {
                    //No-op
                }
                else
                {
                    if (mismatchS1.Length > 0)
                    {
                        retVal.Add(new List() { mismatchS1, mismatchS2 });
                    }
                    stateMatches = true;
                }
            }
            else
            {
                if (stateMatches)
                {
                    mismatchS1 = s1[i].ToString();
                    mismatchS2 = s2[i].ToString();
                    stateMatches = false;
                }
                else
                {
                    mismatchS1 += s1[i].ToString();
                    mismatchS2 += s2[i].ToString();
                }
            }
        }
        if (!stateMatches)
        {
            if (mismatchS1.Length > 0)
            {
                retVal.Add(new List() { mismatchS1, mismatchS2 });
            }
        }

        return retVal;
    }
}

Comments

Popular posts from this blog

Standard Priority Queue IX: Vowels Frequency

Binary Search to Find Next Greater Element V

Simulation: Build Generations