leet-code/src/main/java/leetcode/editor/cn/MinimumNumberOfStepsToMakeTwoStringsAnagram.java
2021-04-29 23:21:52 +08:00

86 lines
2.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//给你两个长度相等的字符串 s 和 t。每一个步骤中你可以选择将 t 中的 任一字符 替换为 另一个字符。
//
// 返回使 t 成为 s 的字母异位词的最小步骤数。
//
// 字母异位词 指字母相同,但排列不同(也可能相同)的字符串。
//
//
//
// 示例 1
//
// 输出s = "bab", t = "aba"
//输出1
//提示:用 'b' 替换 t 中的第一个 'a't = "bba" 是 s 的一个字母异位词。
//
//
// 示例 2
//
// 输出s = "leetcode", t = "practice"
//输出5
//提示:用合适的字符替换 t 中的 'p', 'r', 'a', 'i' 和 'c',使 t 变成 s 的字母异位词。
//
//
// 示例 3
//
// 输出s = "anagram", t = "mangaar"
//输出0
//提示:"anagram" 和 "mangaar" 本身就是一组字母异位词。
//
//
// 示例 4
//
// 输出s = "xxyyzz", t = "xxyyzz"
//输出0
//
//
// 示例 5
//
// 输出s = "friend", t = "family"
//输出4
//
//
//
//
// 提示:
//
//
// 1 <= s.length <= 50000
// s.length == t.length
// s 和 t 只包含小写英文字母
//
// Related Topics 字符串
// 👍 24 👎 0
package leetcode.editor.cn;
//1347:制造字母异位词的最小步骤数
public class MinimumNumberOfStepsToMakeTwoStringsAnagram {
public static void main(String[] args) {
//测试代码
Solution solution = new MinimumNumberOfStepsToMakeTwoStringsAnagram().new Solution();
solution.minSteps("anagram", "mangaar");
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int minSteps(String s, String t) {
int[] arr = new int[26];
int length = s.length();
int num = 0;
for (int i = 0; i < length; i++) {
if(arr[s.charAt(i)-'a']>=0){
num++;
}
arr[s.charAt(i)-'a']++;
if(arr[t.charAt(i)-'a']>0){
num--;
}
arr[t.charAt(i)-'a']--;
}
return num;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}