当前位置:网站首页>854. 相似度为 K 的字符串 BFS
854. 相似度为 K 的字符串 BFS
2022-07-05 21:30:00 【钰娘娘】
854. 相似度为 K 的字符串
字符串
s1和s2是k相似 的(对于某些非负整数k),如果我们可以交换s1中两个字母的位置正好k次,使结果字符串等于s2。给定两个字谜游戏
s1和s2,返回s1和s2与k相似 的最小k。示例 1:
输入:s1 = "ab", s2 = "ba" 输出:1示例 2:
输入:s1 = "abc", s2 = "bca" 输出:2提示:
1 <= s1.length <= 20s2.length == s1.lengths1和s2只包含集合{'a', 'b', 'c', 'd', 'e', 'f'}中的小写字母s2是s1的一个字谜
做题结果
失败,dfs一直超时,头疼
方法:BFS
1. 起点步数为0,化解为求a变成b的最少步数问题
2. 每次用其中某个相同字符进行填写,挪动到对应位置
3. 找到了直接返回
class Solution {
public int kSimilarity(String s1, String s2) {
Queue<String> queue = new LinkedList<>();
Map<String,Integer> distances = new HashMap<>();
distances.put(s1,0);
queue.offer(s1);
while (!queue.isEmpty()){
String str = queue.poll();
int nextStep = distances.get(str)+1;
for(String next:getNext(str,s2)){
if(next.equals(s2)) return nextStep;
if(!distances.containsKey(next)){
distances.put(next,nextStep);
queue.offer(next);
}
}
}
return 0;
}
private List<String> getNext(String s,String hope){
List<String> ans = new ArrayList<>();
char[] cs1 = s.toCharArray();
char[] cs2 = hope.toCharArray();
int n = cs1.length;
int i = 0;
while (i<n&&cs1[i]==cs2[i]) ++i;
for(int j = i+1; j < n; j++){
if(cs1[j]!=cs2[j]&&cs1[j]==cs2[i]){
swap(cs1,i,j);
ans.add(new String(cs1));
swap(cs1,i,j);
}
}
return ans;
}
private void swap(char[] cs, int i, int j){
char temp = cs[i];
cs[i] = cs[j];
cs[j] = temp;
}
}边栏推荐
- Explain various hot issues of Technology (SLB, redis, mysql, Kafka, Clickhouse) in detail from the architecture
- Learning notes of statistical learning methods -- Chapter 1 Introduction to statistical learning methods
- Cross end solution to improve development efficiency rapidly
- Traps in the explode function in PHP
- Clion-MinGW编译后的exe文件添加ico图标
- 终端安全能力验证环境搭建和渗透测试记录
- WPF gets the control in the datagridtemplatecolumn of the specified row and column in the DataGrid
- [daily training] 729 My schedule I
- 字典树简单入门题(居然是蓝题?)
- 張麗俊:穿透不確定性要靠四個“不變”
猜你喜欢

让开发效率飞速提升的跨端方案

Feng Tang's "spring breeze is not as good as you" digital collection, logged into xirang on July 8!

秋招将临 如何准备算法面试、回答算法面试题

ArcGIS\QGIS无插件加载(无偏移)MapBox高清影像图
![R language [data management]](/img/41/b89bb8794c06280e58988e1c1a5e02.png)
R language [data management]

LeetCode_哈希表_困难_149. 直线上最多的点数

Explain various hot issues of Technology (SLB, redis, mysql, Kafka, Clickhouse) in detail from the architecture

The transformation based on vertx web sstore redis to realize the distributed session of vertx HTTP application

XML modeling

Why can't Chinese software companies produce products? Abandon the Internet after 00; Open source high-performance API gateway component of station B | weekly email exclusive to VIP members of Menon w
随机推荐
Prior knowledge of machine learning in probability theory (Part 1)
Two ways to realize video recording based on avfoundation
Sophomore personal development summary
Golang(1)|从环境准备到快速上手
Hdu2377bus pass (build more complex diagram +spfa)
Arcgis\qgis no plug-in loading (no offset) mapbox HD image map
sql常用语法记录
Making global exception handling classes with aspect
Learning robots have no way to start? Let me show you the current hot research directions of robots
JMeter installation under win7
思特奇加入openGauss开源社区,共同推动数据库产业生态发展
Display DIN 4102-1 Class B1 fire test requirements
Explain various hot issues of Technology (SLB, redis, mysql, Kafka, Clickhouse) in detail from the architecture
kingbaseES V8R3数据安全案例之---审计记录清除案例
显示器要申请BS 476-7 怎么送样?跟显示屏一样吗??
Teach yourself to train pytorch model to Caffe (2)
Sitge joined the opengauss open source community to jointly promote the ecological development of the database industry
AITM2-0002 12s或60s垂直燃烧试验
Ethereum ETH的奖励机制
WPF gets the control in the datagridtemplatecolumn of the specified row and column in the DataGrid