LeetCode 1347. Minimum Number of Steps to Make Two Strings Anagram

题目

用hash,比较两个字符串数组的每个字符的hash值

class Solution {
public:
    int m[300];
    int m2[300];
    int minSteps(string s, string t) {

        for(int i=0;i<s.length();i++)
        {
            m[s[i]]++;
        }

        for(int i=0;i<t.length();i++)
        {
            m2[t[i]]++;
        }

        int ans=0;
        int steps=0;
        for(int i=0;i<300;i++)
        {

            if(m2[i]<m[i])
            {
                steps += m[i]-m2[i];
            }
            if(m2[i]>m[i])
            {
                steps -= m2[i] - m[i];
                ans+=m2[i]-m[i];
            }
        }

        return ans;

    }
};

原文地址:https://www.cnblogs.com/dacc123/p/12288361.html

时间: 2024-08-28 22:46:39

LeetCode 1347. Minimum Number of Steps to Make Two Strings Anagram的相关文章

【leetcode】1347. Minimum Number of Steps to Make Two Strings Anagram

题目如下: Given two equal-size strings s and t. In one step you can choose any character of t and replace it with another character. Return the minimum number of steps to make t an anagram of s. An Anagram of a string is a string that contains the same c

codeforce 804B Minimum number of steps

cf劲啊 原题: We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each step we choose one of substrings "ab" in the string and replace it with the string "bba". If we have no "ab" as a substring,

Codeforces Round #411 (Div. 2)D. Minimum number of steps(贪心)

传送门 Description We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each step we choose one of substrings "ab" in the string and replace it with the string "bba". If we have no "ab" as a subs

LeetCode | 0452. Minimum Number of Arrows to Burst Balloons用最少数量的箭引爆气球【Python】

LeetCode 0452. Minimum Number of Arrows to Burst Balloons用最少数量的箭引爆气球[Medium][Python][区间贪心] Problem LeetCode There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of

[LeetCode] 452 Minimum Number of Arrows to Burst Balloons

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of s

【leetcode】1342. Number of Steps to Reduce a Number to Zero

题目如下: Given a non-negative integer num, return the number of steps to reduce it to zero. If the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it. Example 1: Input: num = 14 Output: 6 Explanation:  Step 1)

[LeetCode] 871. Minimum Number of Refueling Stops 最少的加油站个数

A car travels from a starting position to a destination which is?target?miles east of the starting position. Along the way, there are gas stations.? Each?station[i]?represents a gas station that is?station[i][0]?miles east of the starting position, a

LeetCode 1284. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix (最少翻转次数将二进制矩阵全部置为0)

给一个矩阵mat,每个格子都是0或1,翻转一个格子会将该格子以及相邻的格子(有共同边)全部翻转(0变为1,1变为0) 求问最少需要翻转几次将所有格子全部置为0. 这题的重点是数据范围,比赛结束看了眼数据范围想把自己锤死= = m == mat.length n == mat[0].length 1 <= m <= 3 1 <= n <= 3 mat[i][j] is 0 or 1. 也就是....最多也就9个格子.....暴力怎么都能搞出来的..... 首先分析每个格子要么不反转,

CF804B Minimum number of steps

思路: 找规律.参考了http://blog.csdn.net/harlow_cheng/article/details/71190999. 实现: 1 #include <iostream> 2 using namespace std; 3 const int mod = 1e9 + 7; 4 int main() 5 { 6 string s; 7 cin >> s; 8 int n = s.length(); 9 int cnt = 1; 10 int sum = 0; 11