当前位置:网站首页>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;
}
}边栏推荐
- libcurl 7.61.0 VS2013 编译教程
- In the era of data processing, data analysis has become the basic construction
- Adobe Premiere基础-不透明度(蒙版)(十一)
- Openssl1.1.1 VS2013-编译教程
- RK1126 新添加一个模块
- Common methods of stream flow lambder
- Anchor type and row data type of DB2 SQL pl
- AEC: analysis of echo generation causes and echo cancellation principle
- Wireshark learning notes (I) common function cases and skills
- Chapter 161 SQL function year
猜你喜欢

Adobe Premiere基础(轨道相关)(五)

Adobe Premiere foundation - tool use (selection tool, razor tool, and other common tools) (III)

What is Bi? Talk about the definition and function of Bi

锐捷x32pro刷openwrt开启无线160MHz

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

3. Golang并发入门
![[vulnhub range] janchow: 1.0.1](/img/b5/e3f0d213ee87cd60802ee3db79d10f.png)
[vulnhub range] janchow: 1.0.1

Seata installing the window environment

AEC: analysis of echo generation causes and echo cancellation principle

第一章 SQL操作符
随机推荐
How can bi help enterprises reduce labor, time and management costs?
Common methods of stream flow lambder
Adobe Premiere基础(动画制作-弹性动画)(八)
Request header field xxxx is not allowed by Access-Control-Allow-Headers in preflight response问题
lingo12软件下载及lingo语言入门资源
第6章 关系数据理论练习
Adobe Premiere基础-工具使用(选择工具,剃刀工具,等常用工具)(三)
数据库防火墙闪亮登场(好文共赏)
基于JSP的医院预约挂号平台设计与开发.zip(论文+项目源码)
[kuangbin] topic 22 interval DP
端午“沉浸式云旅游”怎么玩?即构助力“直播+”新场景落地
MySQL高级篇第一章(linux下安装MySQL)【上】
直播预告 | 解构OLAP!新型多维分析架构范式全公开!Apache Doris 将带来五个重磅议题!
In the era of data processing, data analysis has become the basic construction
超级简单的课程设计ssm学生管理系统(含源码简单添加、删除、修改、查询操作)
AgI foundation, uncertain reasoning, subjective logic Ppt1
Openssl1.1.1 vs2013 compilation tutorial
基于谱加权的波束方向图分析
Adobe Premiere基础(轨道相关)(五)
Upgrade the playing method of snatching singing, integrate the climax clips of genuine music and real-time scoring ability~