当前位置:网站首页>854. String BFS with similarity K
854. String BFS with similarity K
2022-07-05 21:45:00 【Empress Yu】
854. The similarity is K String
character string
s1
ands2
yesk
be similar Of ( For some nonnegative integersk
), If we could exchanges1
The two letters in the are in the right placek
Time , Make the result string equal tos2
.Given two word puzzles
s1
ands2
, returns1
ands2
Andk
be similar Minimumk
.Example 1:
Input :s1 = "ab", s2 = "ba" Output :1Example 2:
Input :s1 = "abc", s2 = "bca" Output :2Tips :
1 <= s1.length <= 20
s2.length == s1.length
s1
ands2
Contains only sets{'a', 'b', 'c', 'd', 'e', 'f'}
Small letters ins2
yess1
A word puzzle
The result of doing the question
Failure ,dfs All the time , Have a headache
Method :BFS
1. The starting steps are 0, Resolve for the sake of seeking a become b The minimum number of steps of
2. Fill in one of the same characters each time , Move to the corresponding position
3. Find it and go back
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;
}
}
边栏推荐
- Gcc9.5 offline installation
- How can Huawei online match improve the success rate of player matching
- crm创建基于fetch自己的自定义报告
- int GetMonth( ) const throw( );后面的throw( )什么意思?
- Ethereum ETH的奖励机制
- Teach yourself to train pytorch model to Caffe (2)
- Codeforces 12D Ball 树形阵列模拟3排序元素
- MQ----activeMq
- Simple interest mode - evil Chinese style
- 張麗俊:穿透不確定性要靠四個“不變”
猜你喜欢
Haas506 2.0 development tutorial - Alibaba cloud OTA - PAC firmware upgrade (only supports versions above 2.2)
Golang(1)|从环境准备到快速上手
ICMP 介绍
1.2 download and installation of the help software rstudio
Alibaba cloud award winning experience: build a highly available system with polardb-x
Oracle checkpoint queue - Analysis of the principle of instance crash recovery
Uni app Bluetooth communication
Parker driver maintenance COMPAX controller maintenance cpx0200h
Teach yourself to train pytorch model to Caffe (III)
Defect detection - Halcon surface scratch detection
随机推荐
SQL common syntax records
Detailed explanation of memset() function usage
Oracle检查点队列–实例崩溃恢复原理剖析
The solution to the problem that Oracle hugepages are not used, causing the server to be too laggy
EBS Oracle 11g 克隆步骤(单节点)
初级软件测试必问面试题
Emotional analysis of wechat chat records on Valentine's day based on Text Mining
GCC9.5离线安装
CRM creates its own custom report based on fetch
leetcode:1755. Sum of subsequences closest to the target value
华为游戏多媒体服务调用屏蔽指定玩家语音方法,返回错误码3010
Deeply convinced plan X - network protocol basic DNS
Longest swing sequence [greedy practice]
Interview questions for basic software testing
Teach yourself to train pytorch model to Caffe (2)
基于 Ingress Controller 在集群外访问 Zadig 自测环境(最佳实践)
Oracle HugePages没有被使用导致服务器很卡的解决方法
Haas506 2.0 development tutorial - Alibaba cloud OTA - PAC firmware upgrade (only supports versions above 2.2)
Gcc9.5 offline installation
Poj 3237 Tree (Tree Chain Split)