Bear and Steady Gene


Problem Statement :


A gene is represented as a string of length  (where  is divisible by ), composed of the letters , , , and . It is considered to be steady if each of the four letters occurs exactly  times. For example,  and  are both steady genes.

Bear Limak is a famous biotechnology scientist who specializes in modifying bear DNA to make it steady. Right now, he is examining a gene represented as a string . It is not necessarily steady. Fortunately, Limak can choose one (maybe empty) substring of  and replace it with any string of the same length.

Modifying a large substring of bear genes can be dangerous. Given a string , can you help Limak find the length of the smallest possible substring that he can replace to make  a steady gene?

Note: A substring of a string  is a subsequence made up of zero or more contiguous characters of .

As an example, consider . The substring  just before or after  can be replaced with  or . One selection would create .

Function Description

Complete the  function in the editor below. It should return an integer that represents the length of the smallest substring to replace.

steadyGene has the following parameter:

gene: a string
Input Format

The first line contains an interger  divisible by , that denotes the length of a string .
The second line contains a string  of length n.


Output Format

Print the length of the minimum length substring that can be replaced to make  gene stable.



Solution :



title-img


                            Solution in C :

In   C++ :








#include <bits/stdc++.h>
#define F first
#define S second
#define X real()
#define Y imag()
using namespace std;
typedef long long ll;
typedef long double ld;

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n;
	string s;
	cin>>n>>s;
	int a=0;
	int c=0;
	int t=0;
	int g=0;
	for (int i=0;i<n;i++){
		if (s[i]=='A') a++;
		if (s[i]=='C') c++;
		if (s[i]=='T') t++;
		if (s[i]=='G') g++;
	}
	a-=n/4;
	c-=n/4;
	t-=n/4;
	g-=n/4;
	int i2=0;
	int ca=0;
	int cc=0;
	int ct=0;
	int cg=0;
	int v=n;
	for (int i=0;i<n;i++){
		while (i2<n&&(ca<a||cc<c||ct<t||cg<g)){
			if (s[i2]=='A') ca++;
			if (s[i2]=='C') cc++;
			if (s[i2]=='T') ct++;
			if (s[i2]=='G') cg++;
			i2++;
		}
		if (!(ca<a||cc<c||ct<t||cg<g)){
			v=min(v, i2-i);
		}
		if (s[i]=='A') ca--;
		if (s[i]=='C') cc--;
		if (s[i]=='T') ct--;
		if (s[i]=='G') cg--;
	}
	cout<<v<<endl;
}










In    Java  :







import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
    
    public static int code(char c) {
        if (c == 'A') return 0;
        if (c == 'C') return 1;
        if (c == 'G') return 2;
        return 3;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        in.nextLine();
        String str = in.nextLine();
        int j = n-1;
        int[] count = new int[4];
        while (true) {
            if (j == -1) {
                System.out.println("0");
                return;
            }
            if (count[code(str.charAt(j))] + 1 > n/4) {
                j++;
                break;
            }
            count[code(str.charAt(j))]++;
            j--;
        }
        int result = j;
        for (int i = 0; i < n; i++) {
            count[code(str.charAt(i))]++;
            while(count[code(str.charAt(i))] > n/4) {
                if (j == n) {
                    System.out.println(result);
                    return;
                }
                count[code(str.charAt(j))]--;
                j++;
            }
            result = Math.min(result, j - i - 1);
        }
        System.out.println(result);
    }
}










In   C  :







#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <stdbool.h>

#define MAX_L 500002

static int cvt(int c)
{
	switch (c) {
	case 'A':
		return 0;
	case 'T':
		return 1;
	case 'C':
		return 2;
	case 'G':
		return 3;
	}
	abort();
}

static bool check(int cnts[][MAX_L + 1], int n, int i, int l)
{
	bool ok = 1;
	int sum = 0;
	for (int j = 0; j < 4; j++) {
		int diff = n/4 - (cnts[j][n] - (cnts[j][i + l] - cnts[j][i]));
		ok &= diff >= 0;
		sum += diff;
	}
	return ok && sum == l;
}

static int subst(char s[MAX_L], int n)
{
	int res = INT_MAX;
	static int cnts[4][MAX_L + 1];
	for (int i = 1; i <= n; i++) {
		for (int j = 0; j < 4; j++) {
			cnts[j][i] = cnts[j][i-1];
		}
		cnts[s[i-1]][i] += 1;
	}

	for (int i = 0; i < n; i++) {
		int len = 1;
		int l, r;
		while (i + len <= n) {
			if (check(cnts, n, i, len))
				break;
			len *= 2;
		}
		if (i + len > n) {
			len = n - i;
			if (!check(cnts, n, i, len))
				continue;
		}
		l = len / 2;
		r = len;
		while (l < r) {
			int m = l + (r - l)/2;
			if (check(cnts, n, i, m)) {
				r = m;
				len = r;
			} else {
				l = m + 1;
			}
		}
		if (len < res)
			res = len;
	}
	return res;
}

int main()
{
	int n;
	static char s[MAX_L];
	fgets(s, MAX_L, stdin);
	sscanf(s, "%d", &n);
	fgets(s, MAX_L, stdin);
	for (int i = 0; i < n; i++) {
		s[i] = cvt(s[i]);
	}
	printf("%d", subst(s, n));
	return 0;
}









In   Python3  :







from collections import Counter

n = int(input())
m = n // 4
s = input()
cnt = {'G': 0, 'A': 0, 'C': 0, 'T': 0}
cnt.update(Counter(s))

if all(map(lambda x: x == m, cnt.values())):
    print(0)
    exit()
low, high = 0, n
while high - low > 1:
    mid = (high + low) // 2
    cnt_tmp = dict(cnt)
    for i in range(mid):
        cnt_tmp[s[i]] -= 1
    if all(map(lambda x: x <= m, cnt_tmp.values())):
        high = mid
        continue
    for i in range(mid, n):
        cnt_tmp[s[i - mid]] += 1
        cnt_tmp[s[i]] -= 1
        if all(map(lambda x: x <= m, cnt_tmp.values())):
            high = mid
            break
    else:
        low = mid
print(high)
                        








View More Similar Problems

AND xor OR

Given an array of distinct elements. Let and be the smallest and the next smallest element in the interval where . . where , are the bitwise operators , and respectively. Your task is to find the maximum possible value of . Input Format First line contains integer N. Second line contains N integers, representing elements of the array A[] . Output Format Print the value

View Solution →

Waiter

You are a waiter at a party. There is a pile of numbered plates. Create an empty answers array. At each iteration, i, remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible ith the prime number. If it is, stack it in pile Bi. Otherwise, stack it in stack Ai. Store the values Bi in from top to bottom in answers. In the next iteration, do the

View Solution →

Queue using Two Stacks

A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que

View Solution →

Castle on the Grid

You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):

View Solution →

Down to Zero II

You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.

View Solution →

Truck Tour

Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petr

View Solution →