小美的 MT

小美的MT
MT是美团的缩写,因此小美很喜欢这两个字母。现在小美拿到了一个仅由大写字母组成字符串,她可以最多操作k次,每次可以修改任意一个字符。小美想知道,操作结束后最多共有多少个’M’和’T’字符?



输入描述:

第一行输入两个正整数n k,代表字符串长度和操作次数

第二行输入一个长度为n的、仅由大写字母组成的字符串.

1 <= k <= n <= 10^5



输出描述:

```
输出操作结束后最多共有多少个'M'和'T'字符。
```



**样例**

输入:

5 2
MTUAN

输出:

4

例子说明:

```
修改第三个和第五个字符,形成的字符串为 MTTAM,这样共有 4 个'M'和'T'。
```



#### 我的答案

```
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        String str = in.nextLine();
        String nums[] = str.split(" ");
        int n = Integer.parseInt(nums[0]);
        int k = Integer.parseInt(nums[1]);
        String value = in.nextLine();

        int mCount = 0;
        int tCount = 0;
        int othersCount = 0;
        int i = 0;
        while (i < value.length()) {
            char ch = value.charAt(i);
            if (ch == 'M') {
                mCount++;
            } else if (ch == 'T') {
                tCount++;
            } else {
                othersCount++;
            }

            i++;
        }

        i = 0;
        while (i < k) {
            i++;
            
            if (othersCount > 0) {
                othersCount--;
                if (mCount >= tCount) {
                    tCount++;
                } else {
                    mCount++;
                }
            } else {
                if (Math.abs(mCount - tCount) >= 2) {
                    if (mCount > tCount) {
                        mCount--;
                        tCount++;
                    } else {
                        mCount++;
                        tCount--;
                    }
                } else {
                    break;
                }
            }
        }


        System.out.println(Math.min(mCount, tCount) * 2);
        // while (in.hasNextInt()) { // 注意 while 处理多个 case
        //     int a = in.nextInt();
        //     int b = in.nextInt();
        //     System.out.println(a + b);
        // }
    }
}
```

未通过。





#### 参考答案

##### 分析

字符串长度为`len`,先遍历找到字符串中M和T字符的个数`res` ,那么小美最多可以将`min((len - res), k)`个字符修改为M和T。最后将`res` 和 `min((len - res), k)`相加即为操作结束后最多共有’M’和’T’字符的个数。

```
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = scanner.nextInt();
        String s = scanner.next();
        int len = s.length();
        //res是结果,也就是M和T字符的总个数
        int res = 0;
        //遍历字符串,记录下M和T字符当前的个数
        for (char c : s.toCharArray()) {
            if (c == 'M' || c == 'T') {
                res++;
            }
        }
        //最终结果是res加上k与(len - res)的最小值
        res = res + Math.min((len - res), k);
        System.out.println(res);
    }
}

```

[【美团20240309笔试算法题】小美的MT_mt 是美团的缩写,因此小美很喜欢这两个字母。 现在小美拿到了一个仅由大写字母组-CSDN博客](https://blog.csdn.net/m0_60511809/article/details/136620192)


全部评论

相关推荐

不愿透露姓名的神秘牛友
06-24 20:25
腾讯今年实习招了这么多人,后面秋招还会招人吗??想着秋招再战来着
牛客96559368...:腾讯好像2020年之后就是实习生招得多,应届生基本上不招,纯实习转正
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务