java.lang.Object
g2301_2400.s2301_match_substring_after_replacement.Solution

public class Solution extends Object
2301 - Match Substring After Replacement.<p>Hard</p> <p>You are given two strings <code>s</code> and <code>sub</code>. You are also given a 2D character array <code>mappings</code> where <code>mappings[i] = [old<sub>i</sub>, new<sub>i</sub>]</code> indicates that you may <strong>replace</strong> any number of <code>old<sub>i</sub></code> characters of <code>sub</code> with <code>new<sub>i</sub></code>. Each character in <code>sub</code> <strong>cannot</strong> be replaced more than once.</p> <p>Return <code>true</code> <em>if it is possible to make</em> <code>sub</code> <em>a substring of</em> <code>s</code> <em>by replacing zero or more characters according to</em> <code>mappings</code>. Otherwise, return <code>false</code>.</p> <p>A <strong>substring</strong> is a contiguous non-empty sequence of characters within a string.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;fool3e7bar&rdquo;, sub = &ldquo;leet&rdquo;, mappings = [[&ldquo;e&rdquo;,&ldquo;3&rdquo;],[&ldquo;t&rdquo;,&ldquo;7&rdquo;],[&ldquo;t&rdquo;,&ldquo;8&rdquo;]]</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong> Replace the first &lsquo;e&rsquo; in sub with &lsquo;3&rsquo; and &lsquo;t&rsquo; in sub with &lsquo;7&rsquo;.</p> <p>Now sub = &ldquo;l3e7&rdquo; is a substring of s, so we return true.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;fooleetbar&rdquo;, sub = &ldquo;f00l&rdquo;, mappings = [[&ldquo;o&rdquo;,&ldquo;0&rdquo;]]</p> <p><strong>Output:</strong> false</p> <p><strong>Explanation:</strong> The string &ldquo;f00l&rdquo; is not a substring of s and no replacements can be made.</p> <p>Note that we cannot replace &lsquo;0&rsquo; with &lsquo;o&rsquo;.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> s = &ldquo;Fool33tbaR&rdquo;, sub = &ldquo;leetd&rdquo;, mappings = [[&ldquo;e&rdquo;,&ldquo;3&rdquo;],[&ldquo;t&rdquo;,&ldquo;7&rdquo;],[&ldquo;t&rdquo;,&ldquo;8&rdquo;],[&ldquo;d&rdquo;,&ldquo;b&rdquo;],[&ldquo;p&rdquo;,&ldquo;b&rdquo;]]</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong> Replace the first and second &lsquo;e&rsquo; in sub with &lsquo;3&rsquo; and &lsquo;d&rsquo; in sub with &lsquo;b&rsquo;.</p> <p>Now sub = &ldquo;l33tb&rdquo; is a substring of s, so we return true.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= sub.length <= s.length <= 5000</code></li> <li><code>0 <= mappings.length <= 1000</code></li> <li><code>mappings[i].length == 2</code></li> <li><code>old<sub>i</sub> != new<sub>i</sub></code></li> <li><code>s</code> and <code>sub</code> consist of uppercase and lowercase English letters and digits.</li> <li><code>old<sub>i</sub></code> and <code>new<sub>i</sub></code> are either uppercase or lowercase English letters or digits.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • matchReplacement

      public boolean matchReplacement(String s, String sub, char[][] mappings)