Open In App

CSES Solutions – Repetitions

Last Updated : 16 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

You are given a DNA sequence: a string S consisting of characters A, C, G, and T. Your task is to find the longest repetition in the sequence. This is a maximum-length substring containing only one type of character.

Examples:

Input: S = “ATTCGGGA”
Output: 3
Explanation: The longest repetition in the sequence is “GGG” which has a length of 3.

Input: S = “AATTGGCCCC”
Output: 4
Explanation: The longest repetition in the sequence is “CCCC” which has a length of 4.

Approach: To solve the problem, follow the below idea:

The problem can be solved by maintaining the running count of contiguous characters. Iterate over the string and if the current character is same as the previous character, then we can increment the count by 1 otherwise we can reset the count to 1. Also maintain another variable to store the maximum value of count in any iteration.

Step-by-step algorithm:

  • Maintain a variable count to store the running count of the contiguous character.
  • Iterate over the string, if the current character is same as the previous character, increment count by 1.
  • Otherwise reset count to 1.
  • Also keep checking for the maximum value of count and store it in ans.
  • After iterating over the entire string, return ans as the final result.

Below is the implementation of the algorithm:

C++
#include <bits/stdc++.h>
using namespace std;

int solve(string S)
{
    int ans = 1, count = 1;
    for (int i = 1; i < S.length(); i++) {
        // If the current character is same as the previous
        // character, then increment the count
        if (S[i] == S[i - 1])
            count += 1;
        else
            // If the current character is different from
            // the previous character, then reset count to 1
            count = 1;
        ans = max(ans, count);
    }
    return ans;
}

int main()
{
    // Input
    string S = "AATTGGCCCC";

    cout << solve(S) << endl;
    return 0;
}
Java
public class Main {
    
    static int solve(String S) {
        int ans = 1, count = 1;
        for (int i = 1; i < S.length(); i++) {
            // If the current character is same as the previous
            // character, then increment the count
            if (S.charAt(i) == S.charAt(i - 1))
                count += 1;
            else
                // If the current character is different from
                // the previous character, then reset count to 1
                count = 1;
            ans = Math.max(ans, count);
        }
        return ans;
    }
    
    public static void main(String[] args) {
        // Input
        String S = "AATTGGCCCC";

        System.out.println(solve(S));
    }
}
C#
using System;

class GFG
{
    static int Solve(string S)
    {
        int ans = 1, count = 1;
        for (int i = 1; i < S.Length; i++)
        {
            // If the current character is same as the previous
            // character then increment the count
            if (S[i] == S[i - 1])
                count += 1;
            else
                // If the current character is different from
                // previous character then reset count to 1
                count = 1;
            
            ans = Math.Max(ans, count);
        }
        return ans;
    }
    static void Main(string[] args)
    {
        // Input
        string S = "AATTGGCCCC";

        Console.WriteLine(Solve(S));
    }
}
Javascript
function GFG(S) {
    let ans = 1;
    let count = 1;
    for (let i = 1; i < S.length; i++) {
        // If the current character is same as the previous character
        // then increment the count
        if (S[i] === S[i - 1]) {
            count += 1;
        } else {
            count = 1;
        }
        // Update ans with maximum count encountered so far
        ans = Math.max(ans, count);
    }
    return ans;
}
// Sample Input
const S = "AATTGGCCCC";
console.log(GFG(S));
Python3
def solve(S):
    ans = 1
    count = 1

    for i in range(1, len(S)):
        # If the current character is same as the previous
        # character then increment the count
        if S[i] == S[i - 1]:
            count += 1
        else:
            # If the current character is different from
            # the previous character then reset count to 1
            count = 1

        ans = max(ans, count)

    return ans

# Main function
if __name__ == "__main__":
    # Input
    S = "AATTGGCCCC"

    print(solve(S))
    
# This code is contributed by shivamgupta310570

Output
4


Time Complexity: O(N), where N is the length of input string S.
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads