返回介绍

solution / 2100-2199 / 2186.Minimum Number of Steps to Make Two Strings Anagram II / README_EN

发布于 2024-06-17 01:03:09 字数 4308 浏览 0 评论 0 收藏 0

2186. Minimum Number of Steps to Make Two Strings Anagram II

中文文档

Description

You are given two strings s and t. In one step, you can append any character to either s or t.

Return _the minimum number of steps to make _s_ and _t_ anagrams of each other._

An anagram of a string is a string that contains the same characters with a different (or the same) ordering.

 

Example 1:

Input: s = "leetcode", t = "coats"
Output: 7
Explanation: 
- In 2 steps, we can append the letters in "as" onto s = "leetcode", forming s = "leetcodeas".
- In 5 steps, we can append the letters in "leede" onto t = "coats", forming t = "coatsleede".
"leetcodeas" and "coatsleede" are now anagrams of each other.
We used a total of 2 + 5 = 7 steps.
It can be shown that there is no way to make them anagrams of each other with less than 7 steps.

Example 2:

Input: s = "night", t = "thing"
Output: 0
Explanation: The given strings are already anagrams of each other. Thus, we do not need any further steps.

 

Constraints:

  • 1 <= s.length, t.length <= 2 * 105
  • s and t consist of lowercase English letters.

Solutions

Solution 1

class Solution:
  def minSteps(self, s: str, t: str) -> int:
    cnt = Counter(s)
    for c in t:
      cnt[c] -= 1
    return sum(abs(v) for v in cnt.values())
class Solution {
  public int minSteps(String s, String t) {
    int[] cnt = new int[26];
    for (char c : s.toCharArray()) {
      ++cnt[c - 'a'];
    }
    for (char c : t.toCharArray()) {
      --cnt[c - 'a'];
    }
    int ans = 0;
    for (int v : cnt) {
      ans += Math.abs(v);
    }
    return ans;
  }
}
class Solution {
public:
  int minSteps(string s, string t) {
    vector<int> cnt(26);
    for (char& c : s) ++cnt[c - 'a'];
    for (char& c : t) --cnt[c - 'a'];
    int ans = 0;
    for (int& v : cnt) ans += abs(v);
    return ans;
  }
};
func minSteps(s string, t string) int {
  cnt := make([]int, 26)
  for _, c := range s {
    cnt[c-'a']++
  }
  for _, c := range t {
    cnt[c-'a']--
  }
  ans := 0
  for _, v := range cnt {
    ans += abs(v)
  }
  return ans
}

func abs(x int) int {
  if x < 0 {
    return -x
  }
  return x
}
function minSteps(s: string, t: string): number {
  let cnt = new Array(128).fill(0);
  for (const c of s) {
    ++cnt[c.charCodeAt(0)];
  }
  for (const c of t) {
    --cnt[c.charCodeAt(0)];
  }
  let ans = 0;
  for (const v of cnt) {
    ans += Math.abs(v);
  }
  return ans;
}
/**
 * @param {string} s
 * @param {string} t
 * @return {number}
 */
var minSteps = function (s, t) {
  let cnt = new Array(26).fill(0);
  for (const c of s) {
    ++cnt[c.charCodeAt() - 'a'.charCodeAt()];
  }
  for (const c of t) {
    --cnt[c.charCodeAt() - 'a'.charCodeAt()];
  }
  let ans = 0;
  for (const v of cnt) {
    ans += Math.abs(v);
  }
  return ans;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文