A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/competitive-programming/cses-solutions-repetitions/ below:

CSES Solutions - Repetitions - GeeksforGeeks

CSES Solutions - Repetitions

Last Updated : 16 Mar, 2024

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:

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

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



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4