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:
- 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
Time Complexity: O(N), where N is the length of input string S.
Auxiliary Space: O(1)
Similar Reads
CSES Problem Set Solutions In this article, we have compiled comprehensive, high-quality tutorials on the CSES Problem Set Solutions to assist you in understanding the problem set for learning algorithmic programming. What is CSES Problem Set?CSES Problem Set is a collection of competitive programming tasks hosted on the CSES
8 min read
Repetition, Pattern, and Rhythm The concepts of repetition, pattern, and rhythm in design are simple concepts but can be powerful tools for designers. These must be studied to create effective designs. Repetition, when used correctly, can strengthen a design and improve user understanding. Mastering these concepts is essential for
8 min read
Class 8 NCERT Solutions- Chapter 14 Factorisation - Exercise 14.4 Exercise 14.3 of Chapter 14 (Factorisation) in the Class 8 NCERT Mathematics textbook focuses on factoring algebraic expressions using various methods. This exercise helps students understand how to break down complex expressions into simpler factors, which is a crucial skill in algebra. Find and co
8 min read
One-Step Equations Practice Questions Basic algebraic equations, known as one-step equations. They can be solved in one step by applying addition, subtraction, multiplication, or division. The objective is to carry out the inverse operation on both sides of the equation in order to isolate the variable. For instance, to find x in an equ
3 min read
Class 11 NCERT Solutions - Chapter 7 Permutations And Combinations - Exercise 7.2 Chapter 7 of the Class 11 NCERT Mathematics textbook, titled "Permutations and Combinations," delves into the principles of counting and arranging objects. This chapter covers the concepts of permutations and combinations, which are crucial for solving problems involving the arrangement and selectio
3 min read