java.lang.Object
g2201_2300.s2287_rearrange_characters_to_make_target_string.Solution

public class Solution extends Object
2287 - Rearrange Characters to Make Target String.<p>Easy</p> <p>You are given two <strong>0-indexed</strong> strings <code>s</code> and <code>target</code>. You can take some letters from <code>s</code> and rearrange them to form new strings.</p> <p>Return <em>the <strong>maximum</strong> number of copies of</em> <code>target</code> <em>that can be formed by taking letters from</em> <code>s</code> <em>and rearranging them.</em></p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;ilovecodingonleetcode&rdquo;, target = &ldquo;code&rdquo;</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong></p> <p>For the first copy of &ldquo;code&rdquo;, take the letters at indices 4, 5, 6, and 7.</p> <p>For the second copy of &ldquo;code&rdquo;, take the letters at indices 17, 18, 19, and 20.</p> <p>The strings that are formed are &ldquo;ecod&rdquo; and &ldquo;code&rdquo; which can both be rearranged into &ldquo;code&rdquo;.</p> <p>We can make at most two copies of &ldquo;code&rdquo;, so we return 2.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;abcba&rdquo;, target = &ldquo;abc&rdquo;</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>We can make one copy of &ldquo;abc&rdquo; by taking the letters at indices 0, 1, and 2.</p> <p>We can make at most one copy of &ldquo;abc&rdquo;, so we return 1.</p> <p>Note that while there is an extra &lsquo;a&rsquo; and &lsquo;b&rsquo; at indices 3 and 4, we cannot reuse the letter &lsquo;c&rsquo; at index 2, so we cannot make a second copy of &ldquo;abc&rdquo;.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> s = &ldquo;abbaccaddaeea&rdquo;, target = &ldquo;aaaaa&rdquo;</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>We can make one copy of &ldquo;aaaaa&rdquo; by taking the letters at indices 0, 3, 6, 9, and 12.</p> <p>We can make at most one copy of &ldquo;aaaaa&rdquo;, so we return 1.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= s.length <= 100</code></li> <li><code>1 <= target.length <= 10</code></li> <li><code>s</code> and <code>target</code> consist of lowercase English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • rearrangeCharacters

      public int rearrangeCharacters(String s, String target)