当前位置:网站首页>2022.05.29(LC_6078_重排字符形成目标字符串)
2022.05.29(LC_6078_重排字符形成目标字符串)
2022-06-10 18:16:00 【Leeli9316】

方法:计数
class Solution {
public int rearrangeCharacters(String s, String target) {
Map<Character, Integer> tMap = new HashMap<>();
for (char ch : target.toCharArray()) {
tMap.put(ch, tMap.getOrDefault(ch, 0) + 1);
}
Map<Character, Integer> sMap = new HashMap<>();
for (char ch : s.toCharArray()) {
sMap.put(ch, sMap.getOrDefault(ch, 0) + 1);
}
int ans = 0;
boolean flag = true;
while (flag) {
for (char ch : tMap.keySet()) {
if (sMap.get(ch) != null && sMap.get(ch) >= tMap.get(ch)) {
sMap.put(ch, sMap.get(ch) - tMap.get(ch));
} else {
flag = false;
break;
}
}
if (flag) ans++;
}
return ans;
}
}
class Solution {
public int rearrangeCharacters(String s, String target) {
int[] count1 = new int[26];
int[] count2 = new int[26];
int ans = Integer.MAX_VALUE;
for (char ch : s.toCharArray()) {
count1[ch - 'a']++;
}
for (char ch : target.toCharArray()) {
count2[ch - 'a']++;
}
for (char ch : target.toCharArray()) {
ans = Math.min(ans, count1[ch - 'a'] / count2[ch - 'a']);
}
return ans;
}
}边栏推荐
- Rk1126 adds a new module
- Adobe Premiere Foundation (track related) (V)
- mysql8.0(新特性小结)
- nodejs-判断系统类型-获取主机名称-执行控制台命令-中文乱码
- Chapter II data type (I)
- 第四章 数据类型(三)
- Chapter 1 SQL operators
- Adobe Premiere foundation - tool use (selection tool, razor tool, and other common tools) (III)
- Three ways generated by stream lambda
- & and||
猜你喜欢

Salesmartly | add a new channel slack to help you close the customer relationship

RK1126 新添加一个模块

nodejs-基本架构分析-解析引擎目录-插件安装-核心模块

mysql8.0(新特性小结)

Pits encountered during the use of ETL (ETL Chinese garbled)

Adobe Premiere Foundation (animation) (VII)

Ruixin micro rk1126 platform platform porting libevent cross compiling libevent

Adobe Premiere基础-不透明度(混合模式)(十二)

In the digital age, why should enterprises make digital transformation?

SaleSmartly | 再添新渠道Slack,助你拉近客户关系
随机推荐
Chapter IV data type (III)
Chapter 161 SQL function year
第二章 数据类型(一)
Db2 SQL PL的锚点类型和行数据类型
Adobe Premiere Foundation (the last step of video subtitle adding) (6)
Design and implementation of online ordering system based on SSM Rar (project source code)
Google Earth engine (GEE) -- Copernicus atmosphere monitoring (CAMs) global aerosol AOI near real-time observation data set
Adobe Premiere基础(轨道相关)(五)
第161章 SQL函数 YEAR
3. Golang并发入门
Adobe Premiere基础(动画制作-弹性动画)(八)
SaleSmartly | 再添新渠道Slack,助你拉近客户关系
Adobe Premiere基礎-工具使用(選擇工具,剃刀工具,等常用工具)(三)
The value of Bi in the enterprise: business analysis and development decision
TestNG的HelloWorld例子以及如何在命令行下运行
【代理】10分钟掌握正向代理和反向代理的本质区别
数据治理经典6大痛点?这本书教你解决
Adobe Premiere基础-时间重映射(十)
Beam pattern analysis based on spectral weighting
Jsp基于ssm项目实验室管理系统设计与现实.doc