当前位置:网站首页>[high frequency interview questions] difficulty 1/5, popular enumeration simulation questions
[high frequency interview questions] difficulty 1/5, popular enumeration simulation questions
2022-06-21 19:32:00 【Gong Shui Sanye's Diary】
Title Description
This is a LeetCode Upper 「1737. The minimum number of characters to be changed to satisfy one of the three conditions 」, The difficulty is 「 secondary 」.
Tag : 「 enumeration 」、「 Count 」、「 simulation 」
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 :
aandbIt's just small letters
Count + enumeration
Use c1 and c2 The string a and b Conduct word frequency statistics respectively , Record string a and b The length of is
and
.
Then enumerate the characters
( Lowercase letters a-z), Count the modification times of the three cases respectively :
- Corresponding conditions
: The purpose is to convert the string a All the characters in become 「 Strictly less than 」 character
, The string b All characters in become 「 Not less than / Greater than or equal to 」 character
. This can be counted separately a Medium size meets 「 Greater than or equal to 」 character
Number of characters , as well as b Medium size meets 「 Less than 」 character
Number , The sum of the two is the minimum number of modifications that satisfy the condition . Be careful , When
( It means enumerating to lowercase letters
) when , Need to skip , Because there is no value size 「 Strictly less than 」 Letter
The characters of , That is, it is impossible to replace a string with all characters 「 Strictly less than 」 Letter
;
- Corresponding conditions
: And conditions
Empathy ;
- Corresponding conditions
: If you want to change all the characters of two characters into
, Where the string a The number of characters to be modified is
, character string b The number of characters to be modified is
, The total number of modifications is
.
Enumerate all the characters
after , The minimum value of all the modifications counted is the answer .
Code :
class Solution {
public int minCharacters(String a, String b) {
int n = a.length(), m = b.length(), ans = 0x3f3f3f3f;
int[] c1 = new int[26], c2 = new int[26];
for (char c : a.toCharArray()) c1[c - 'a']++;
for (char c : b.toCharArray()) c2[c - 'a']++;
for (int i = 0; i < 26 && ans != 0; i++) {
// 3
int ca = n - c1[i], cb = m - c2[i];
ans = Math.min(ans, ca + cb);
if (i == 0) continue;
int r1 = 0, r2 = 0;
// 1
for (int j = i; j < 26; j++) r1 += c1[j];
for (int j = 0; j < i; j++) r1 += c2[j];
// 2
for (int j = i; j < 26; j++) r2 += c2[j];
for (int j = 0; j < i; j++) r2 += c1[j];
ans = Math.min(ans, Math.min(r1, r2));
}
return ans;
}
}
- Time complexity : The complexity of word frequency statistics is
, The complexity of the statistical answer is
, among
Is the character set size
- Spatial complexity :
Last
This is us. 「 Brush through LeetCode」 The first of the series No.1737 piece , The series begins with 2021/01/01, As of the start date LeetCode I have in common 1916 questions , Part of it is a locked question , We will finish all the questions without lock first .
In this series , In addition to explaining the idea of solving problems , And give the most concise code possible . If the general solution is involved, there will be corresponding code templates .
In order to facilitate the students to debug and submit code on the computer , I've built a warehouse :https://github.com/SharingSource/LogicStack-LeetCode .
In the warehouse address , You can see the links to the series 、 The corresponding code of the series 、LeetCode Links to the original problem and other preferred solutions .
边栏推荐
- GoF模式-03-行为型模式(下)
- homeassistant addons
- WMS仓库仓储管理系统源码
- 2022 China eye Expo, Shandong Youth eye health exhibition, vision correction and rehabilitation Exhibition
- 2022年6月25日PMP考试通关宝典-5
- The GLM function of R language is used to build a binary logistic regression model (the family parameter is binomial), and the summary function is used to view the summary statistical information of t
- Summary of the 13th week
- 在Qt中设置程序图标的方法介绍
- 中国两颗风云气象“新星”主要数据产品将向全球用户开放共享
- 【面试高频题】难度 1/5,难度较低的链表面试题
猜你喜欢

力扣今日题1108. IP 地址无效化

使用uniapp框架搭建浙里办微应用(单点登录、埋点、适老化、RPC网关)

Huawei launches new products again? These models have excellent functions

华为鸿蒙认证测试题,你能答对几道?

De « l'entreprise gérée par le village » au « Groupe de 10 milliards de yuans », comment l'industrie Hongxing complète - t - elle le « changement de papillon »?

Literature analysis CiteSpace 6.1.2 download and installation tutorial

From "village run enterprise" to "ten billion group", why did red star industry complete the "butterfly transformation"?

W10添加系统环境变量Path

鸿蒙版“抖音”,这体验感赞

企评家全面解读:【国家电网】中国电力财务有限公司企业成长性
随机推荐
Easy introduction to naturallanguageprocessing series topic 6 code practice -- spelling correction based on language model
R语言使用epiDisplay包的followup.plot函数可视化多个ID(病例)监测指标的纵向随访图、使用line.col参数自定义曲线的颜色(色彩)
GetEmptyBlcoksPre Info
Equals null pointer exception
【面试高频题】难度 1.5/5,常见的二分双指针面试题
这篇寒门博士论文致谢火了:回首望过去,可怜无数山
第十三周小结
2022年6月25日PMP考试通关宝典-3
R语言caTools包进行数据划分、randomForest包构建随机森林模型、使用importance函数计算随机森林模型中每个特征的重要度、varImpPlot函数可视化特征的重要度
[pwn基础]Pwntools学习
Qt Creator 7.0常见问题和常见用法
[high frequency interview questions] difficulty 1.5/5, classic "prefix and + two points" application questions
Delete the specified screen
yolov5训练自己的数据集报错记录
Two problems that may occur in the use of ThreadLocal and thread pool
NPDP|如何做好产品生命周期管理?
Huawei launches new products again? These models have excellent functions
挖财商学院属于证券公司吗?开户安全吗?
Guys, please ask me a question about flynk SQL. I have an FQL statement, insert into C sale
Selection skills of national production reinforced Ethernet switch