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

 Live Demo

#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

Updated on: 08-Oct-2020

197 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements