当前位置:网站首页>Leetcode-1737- minimum number of characters to change if one of the three conditions is met
Leetcode-1737- minimum number of characters to change if one of the three conditions is met
2022-07-27 15:18:00 【Honyelchak】
subject
Here are two strings a and b , Both are made up of small letters . In one step , You can take a or b Medium Any character Change for Any lowercase letter .
The ultimate goal of the operation is to satisfy the following three conditions One of :
aMedium Every letter In the alphabet Strictly less thanbMedium Every letter .bMedium Every letter In the alphabet Strictly less thanaMedium Every letter .aandball from The same Alphabetic composition .
Return to what you need to achieve your goals least Operands .
Example 1:
Input :a = "aba", b = "caa" Output :2 explain : The best solutions to meet each condition are : 1) take b Turn into "ccc",2 operations , Satisfy a Every letter in is less than b Every letter in ; 2) take a Turn into "bbb" And will b Turn into "aaa",3 operations , Satisfy b Every letter in is less than a Every letter in ; 3) take a Turn into "aaa" And will b Turn into "aaa",2 operations , Satisfy a and b It's made up of the same letter . The best solution is just 2 operations ( Meet the conditions 1 Or the conditions 3).
Example 2:
Input :a = "dabadd", b = "cda" Output :3 explain : Meet the conditions 1 The best solution is to b Turn into "eee" .
Tips :
1 <= a.length, b.length <= 105aandbIt's just small letters
Topic link : Address
analysis
1. First, use two arrays Put two strings Hash turn ,<key: Lowercase letters , value: The number of this letter in the string >,
So each array size only needs 26
( I use 27 Because I didn't want to declare two more variables )
2. Traverse hash map, Find the minimum operand
If at this time To meet condition one ,
namely A Every letter in the string is less than B Every letter in the string ,
You need to A In a string Blue Part of the corresponding letters are replaced by green The corresponding letter ( Any replacement ), take B In a string Blue Part of the corresponding letters are replaced by green The corresponding letter ( Any replacement ).
If at this time Meet condition 2 ,
namely B Every letter in the string is less than A Every letter in the string ,
You need to A In a string The green part Replace the corresponding letter with Blue The corresponding letter ( Any replacement ), take B In a string green Part of the corresponding letters are replaced by Blue The corresponding letter ( Any replacement ).

The final effect is shown in the figure below :

A Number of replacements +B Number of replacements = The number of operands required to achieve the goal .
therefore Minimum operand = min(" The operands required to satisfy condition one ", " The operands required to satisfy condition 2 ");
If you want to Meet condition 3 :
for example : take A、B Are replaced by “ It's made up of lowercase letters c The string formed ”
About to remove ‘c’ Replace all letters except with ‘c’ The cost .
for example :"abcde" and "pppc" To meet condition 3, you need to do 4+3 Sub permutation .
On the whole , From lower case letters a Traverse to lowercase letters y( No z, This can be verified by drawing ), Is to find the minimum value under any condition .
Code
class Solution {
public int minCharacters(String a, String b) {
int[] arr = new int[27];
int[] brr = new int[27];
int len_a = a.length(), len_b = b.length(), result = Integer.MAX_VALUE;
for(int i = 0; i < a.length(); i++){
arr[a.charAt(i) - 'a']++;
}
for(int i = 0; i < b.length(); i++){
brr[b.charAt(i) - 'a']++;
}
for(int i = 0; i < 25; i++) {
arr[26] += arr[i];
brr[26] += brr[i];
result = Math.min(Math.min(result, len_a + len_b - arr[i] -brr[i]), Math.min(brr[26] + len_a - arr[26], arr[26] + len_b - brr[26]));
}
return Math.min(result, len_a + len_b - arr[25] -brr[25]);
}
}
边栏推荐
- Notice on printing and distributing the Interim Measures for the administration of green manufacturing pilot demonstration of Shenzhen Bureau of industry and information technology
- 事务_基本演示和事务_默认自动提交&手动提交
- What is the breakthrough point of digital transformation in the electronic manufacturing industry? Lean manufacturing is the key
- Unity性能优化------渲染优化(GPU)之LOD(Level of detail)
- 谷歌团队推出新Transformer,优化全景分割方案|CVPR 2022
- LeetCode 191. Number of 1 Bits(位1的个数) 位运算/easy
- 适配验证新职业来了!华云数据参与国家《信息系统适配验证师国家职业技能标准》编制
- 于不确定中见“安全感” 沃尔沃2022年中问道
- 什么是Tor?Tor浏览器更新有什么用?
- Internship: compilation of other configuration classes
猜你喜欢

3.3-5v conversion

IJCAI 2022杰出论文公布,大陆作者中稿298篇拿下两项第一

基于FIFO IDT7202-12的数字存储示波器

Design scheme of digital oscilloscope based on stm32

Kubernetes CNI classification / operation mechanism

CAN总线的EMC设计方案
仪表放大器和运算放大器优缺点对比

The mobile terminal uses the list component of vantui. When multiple tab items are switched back and forth, the list is loaded many times, resulting in the failure of normal display of data

谷歌团队推出新Transformer,优化全景分割方案|CVPR 2022

AssetBundle如何打包
随机推荐
NEFU119 组合素数【算术基本定理】
DIY ultra detailed tutorial on making oscilloscope: (1) I'm not trying to make an oscilloscope
什么是Tor?Tor浏览器更新有什么用?
LeetCode 191. Number of 1 Bits(位1的个数) 位运算/easy
LeetCode 781. 森林中的兔子 哈希表/数学问题 medium
《剑指Offer》 链表反转
Unity3D学习笔记10——纹理数组
LeetCode 74. 搜索二维矩阵 二分/medium
Introduction of the connecting circuit between ad7606 and stm32
Nefu119 combinatorial prime [basic theorem of arithmetic]
Unity mouse controls the first person camera perspective
基于FIFO IDT7202-12的数字存储示波器
TL431-2.5v基准电压芯片几种基本用法
一些二进制位操作
3.3-5v conversion
网络设备硬核技术内幕 路由器篇 9 CISCO ASR9900拆解 (二)
Unity性能优化------渲染优化(GPU)之LOD(Level of detail)
适配验证新职业来了!华云数据参与国家《信息系统适配验证师国家职业技能标准》编制
MySQL 面试40连问,面试官你再问下去我可要翻脸了
OBS advanced DXGI acquisition screen process, and how to modify it to its own cursor