java.lang.Object
g2001_2100.s2068_check_whether_two_strings_are_almost_equivalent.Solution

public class Solution extends Object
2068 - Check Whether Two Strings are Almost Equivalent.<p>Easy</p> <p>Two strings <code>word1</code> and <code>word2</code> are considered <strong>almost equivalent</strong> if the differences between the frequencies of each letter from <code>'a'</code> to <code>'z'</code> between <code>word1</code> and <code>word2</code> is <strong>at most</strong> <code>3</code>.</p> <p>Given two strings <code>word1</code> and <code>word2</code>, each of length <code>n</code>, return <code>true</code> <em>if</em> <code>word1</code> <em>and</em> <code>word2</code> <em>are <strong>almost equivalent</strong> , or</em> <code>false</code> <em>otherwise</em>.</p> <p>The <strong>frequency</strong> of a letter <code>x</code> is the number of times it occurs in the string.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> word1 = &ldquo;aaaa&rdquo;, word2 = &ldquo;bccb&rdquo;</p> <p><strong>Output:</strong> false</p> <p><strong>Explanation:</strong> There are 4 &rsquo;a&rsquo;s in &ldquo;aaaa&rdquo; but 0 &rsquo;a&rsquo;s in &ldquo;bccb&rdquo;.</p> <p>The difference is 4, which is more than the allowed 3.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> word1 = &ldquo;abcdeef&rdquo;, word2 = &ldquo;abaaacc&rdquo;</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3:</p> <ul> <li> <p>&lsquo;a&rsquo; appears 1 time in word1 and 4 times in word2. The difference is 3.</p> </li> <li> <p>&lsquo;b&rsquo; appears 1 time in word1 and 1 time in word2. The difference is 0.</p> </li> <li> <p>&lsquo;c&rsquo; appears 1 time in word1 and 2 times in word2. The difference is 1.</p> </li> <li> <p>&lsquo;d&rsquo; appears 1 time in word1 and 0 times in word2. The difference is 1.</p> </li> <li> <p>&lsquo;e&rsquo; appears 2 times in word1 and 0 times in word2. The difference is 2.</p> </li> <li> <p>&lsquo;f&rsquo; appears 1 time in word1 and 0 times in word2. The difference is 1.</p> </li> </ul> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> word1 = &ldquo;cccddabba&rdquo;, word2 = &ldquo;babababab&rdquo;</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3:</p> <ul> <li> <p>&lsquo;a&rsquo; appears 2 times in word1 and 4 times in word2. The difference is 2.</p> </li> <li> <p>&lsquo;b&rsquo; appears 2 times in word1 and 5 times in word2. The difference is 3.</p> </li> <li> <p>&lsquo;c&rsquo; appears 3 times in word1 and 0 times in word2. The difference is 3.</p> </li> <li> <p>&lsquo;d&rsquo; appears 2 times in word1 and 0 times in word2. The difference is 2.</p> </li> </ul> <p><strong>Constraints:</strong></p> <ul> <li><code>n == word1.length == word2.length</code></li> <li><code>1 <= n <= 100</code></li> <li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • checkAlmostEquivalent

      public boolean checkAlmostEquivalent(String word1, String word2)