当前位置:网站首页>Leetcode:433. minimal genetic change
Leetcode:433. minimal genetic change
2022-07-27 04:11:00 【uncle_ ll】
433. Minimal genetic change
source : Power button (LeetCode)
link : https://leetcode.cn/problems/minimum-genetic-mutation/
The gene sequence can be expressed as a sequence composed of 8 A string of characters , Each of these characters is ‘A’、‘C’、‘G’ and ‘T’ One of .
Suppose we need to investigate from the gene sequence start Turn into end Genetic changes that occur . A gene change means that a character in the gene sequence has changed .
- for example ,“AACCGGTT” --> “AACCGGTA” It's a genetic change .
There is another gene bank bank All valid genetic changes were recorded , Only the genes in the gene bank are effective gene sequences .( The changed gene must be located in the gene pool bank in )
Here are two gene sequences start and end , And a gene bank bank , Please find out and return what makes start Change to end The minimum number of changes required . If this genetic change cannot be completed , return -1 .
Be careful : Starting gene sequence start The default is valid , But it doesn't have to be in the gene pool .
Example 1:
Input :start = "AACCGGTT", end = "AACCGGTA", bank = ["AACCGGTA"]
Output :1
Example 2:
Input :start = "AACCGGTT", end = "AAACGGTA", bank = ["AACCGGTA","AACCGCTA","AAACGGTA"]
Output :2
Example 3:
Input :start = "AAAAACCC", end = "AACCCCCC", bank = ["AAAACCCC","AAACCCCC","AACCCCCC"]
Output :3
Tips :
- 1 <= n <= 9
solution
- Breadth first traversal BFS: After analysis, we can see that , The title requires that a gene sequence AA Change to another gene sequence BB, We need to meet the following conditions :
- Sequence A And Sequence B There is only one character difference between ;
- Changing characters can only be from ‘A’, ‘C’, ‘G’, ‘T’ To choose from ;
- The transformed sequence B Must be in the string array bank in
If start==end, Go straight back to 0; If end be not in bank Medium or bank The length is empty , return -1; Then start traversing start,start Every character of can happen 4 Change , After the change, it needs to be based on bank Check the legitimacy of the changes , If the change equals end, Returns the number of changes , Therefore, it is necessary to synchronize the number of changes saved by a variable . Be careful , Every time it is determined that the change is legal , But it doesn't mean end When , From you to bank Remove the string , Otherwise, it is easy to form a loop , It keeps cycling .
Code implementation
BFS
python Realization
class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
if start == end:
return 0
if len(bank) == 0 or end not in bank:
return -1
q = [(start, 0)]
while q:
cur_str, step = q.pop(0)
for i, x in enumerate(cur_str):
for y in 'ACGT':
if y != x:
nxt = cur_str[:i] + y + cur_str[i+1:]
if nxt in bank:
if nxt == end:
return step + 1
bank.remove(nxt)
q.append((nxt, step+1))
return -1
c++ Realization
class Solution {
public:
int minMutation(string start, string end, vector<string>& bank) {
unordered_set<string> cnt;
unordered_set<string> visited;
char keys[4] = {
'A', 'C', 'G', 'T'};
for (auto & w : bank) {
cnt.emplace(w);
}
if (start == end) {
return 0;
}
if (!cnt.count(end)) {
return -1;
}
queue<string> qu;
qu.emplace(start);
visited.emplace(start);
int step = 1;
while (!qu.empty()) {
int sz = qu.size();
for (int i = 0; i < sz; i++) {
string curr = qu.front();
qu.pop();
for (int j = 0; j < 8; j++) {
for (int k = 0; k < 4; k++) {
if (keys[k] != curr[j]) {
string next = curr;
next[j] = keys[k];
if (!visited.count(next) && cnt.count(next)) {
if (next == end) {
return step;
}
qu.emplace(next);
visited.emplace(next);
}
}
}
}
}
step++;
}
return -1;
}
};
Complexity analysis
- Time complexity : O ( N ∗ M ∗ C ) O(N*M*C) O(N∗M∗C), N Is string start The length of ,C Is a variable number of times , here C=4,M yes bank The length of ;
- Spatial complexity : O ( N ∗ M ) O(N*M) O(N∗M) , N Is string start The length of ,M yes bank The length of , At most in the queue m Elements , The space of each element is O(n), So the space complexity is zero O ( n × m ) O(n×m) O(n×m).
Reference resources
边栏推荐
- Bean Validation原理篇--07
- A. YES or YES?
- 基于风能转换系统的非线性优化跟踪控制(Matlab代码实现)
- 链表内指定区间反转
- Maximum nesting depth of parentheses
- NFT数字藏品系统开发:老牌文学杂志玩起新潮数字藏品
- 3381. 手机键盘(清华大学考研机试题)
- Big talk · book sharing | lean product development: principles, methods and Implementation
- 电商系统结合商品秒杀活动,VR全景不断带来收益
- Read and understand | how data center supports enterprise digital operation
猜你喜欢

Share the current life -- a six week internship experience of a high school graduate in CCTV

基于风能转换系统的非线性优化跟踪控制(Matlab代码实现)

Apachecon Asia preheating live broadcast incubator theme full review

手动从0搭建ABP框架-ABP官方完整解决方案和手动搭建简化解决方案实践

路由策略第一关

Lixia action | Yuanqi Digitalization: existing mode or open source innovation?

酷雷曼VR全景为你铺设创业之路

Chapter 4 decision tree and random forest

Detailed analysis of trajectory generation tool in psins toolbox

Golang sends email to the mail Library
随机推荐
开机启动流程及营救模式
从根到叶的二进制数之和
VR全景人淘金“小心机”(上)
There is no problem reading from flick CDC to mysql8 and mysql5. What should I do?
What is animation effect? What is the transition effect?
A. YES or YES?
NFT数字藏品系统开发:老牌文学杂志玩起新潮数字藏品
288 page 180000 word intelligent campus overall design directory
搜索旋转排序数组
Collating strings
Search rotation sort array
JVM原理简介
分析一下CSDN大佬写的CAS,可重入锁, 非公平锁
ApacheCon Asia 预热直播之孵化器主题全回顾
0726~ resume sorting interview summary
Apachecon Asia preheating live broadcast incubator theme full review
科目三: 济南章丘五号线
The problem that prevented the installation of umi4 for one day has been solved
C. Cypher
Restful Fast Request 2022.2.2发布,支持批量导出文档