- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find total mutation group of genes in C++
Suppose we have a list of strings called genes where each element has the same length and each element contains characters "A", "C", "G" and/or "T". Now there are some rules −
When two strings s1 and s2 are the same string except for one character, then s1 and s2 are in the same mutation group.
When two strings s1 and s2 are in a group and s2 and s3 are in a group, then s1 and s3 are in the same group.
We have to find the total number of mutation groups we can generate.
So, if the input is like genes = ["ACGT", "ACGC", "ACTT", "TTTT", "TGTT"], then the output will be 2, as There are two mutation groups: ["ACGT", "ACGC", "ACTT"] and ["TTTT", "TTTG"]
To solve this, we will follow these steps −
Define one map called parent
Define a function getPar(), this will take a,
if parent[a] is same as a, then:
return a
parent[a] = getPar(parent[a])
return parent[a]
Define a function unite(), this will take a, b,
parA := getPar(a), parB := getPar(b)
if parA is not equal to parB, then:
parent[parA] := parB
return true
return false
Define a function ok(), this will take a, b,
cnt := 0
for initialize i := 0, when i < size of a, update (increase i by 1), do:
cnt := cnt + (1 when a[i] is not same as b[i], otherwise 0)
return true when cnt is 1, otherwise false
From the main method do the following −
sort the array v
Define one set s by taking elements from v
ret := size of v
for each element it in v, do
parent[it] := it
for each element it in v, do
for initialize j := 0, when j < size of it, update (increase j by 1), do:
temp := it
for each character x in [A, C, G, T]
if x is not equal to it[j], then:
temp[j] := x
if temp is present in s, then:
if unite(temp, it), then:
return ret
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class Solution { public: map <string, string> parent; string getPar(string& a){ if(parent[a] == a) return a; return parent[a] = getPar(parent[a]); } bool unite(string& a, string& b){ string parA = getPar(a); string parB = getPar(b); if(parA != parB){ parent[parA] = parB; return true; } return false; } bool ok(string &a, string& b){ int cnt = 0; for(int i = 0; i < a.size(); i++){ cnt += a[i] != b[i]; } return cnt == 1; } int solve(vector<string> v) { sort(v.begin(), v.end()); set <string> s(v.begin(), v.end()); int ret = v.size(); for(auto& it : v){ parent[it]= it; } for(auto& it : v){ for(int j = 0; j < it.size(); j++){ string temp = it; for(char x : {'A', 'C', 'G', 'T'}){ if(x != it[j]){ temp[j] = x; if(s.count(temp)){ if(unite(temp, it)) ret--; } } } } } return ret; } }; main(){ vector<string> v = {"ACGT", "ACGC", "ACTT", "TTTT", "TGTT"}; Solution(ob); cout << ob.solve(v); }
Input
{"ACGT", "ACGC", "ACTT", "TTTT", "TGTT"}
Output
2
- Related Articles
- How to find the total number of rows per group combination in an R data frame?
- Mutation Testing in C#
- Program to find maximum possible value of smallest group in Python
- Mutation Genetic Change
- Program to find total duration of K most watched shows in Python
- Mutation Test tools in C#
- Minimum Genetic Mutation in C++
- C++ Program to find out the total price
- Program to find latest group of size M using Python
- Program to find total amount of money we have in bank in Python
- Program to find total unique duration from a list of intervals in Python
- Program to find total similarities of a string and its substrings in Python
- How to produce group subtotals and a grand total in Oracle?
- Program to find total cost for completing all shipments in python
- Program to find total area covered by two rectangles in Python
