Python - Non-Overlapping occurrences of N Repeated K character


In this article we have to find the non -overlapping occurrences of N repeated K characters using Python. These kinds of problems are very common while working with strings in Python.

Understanding the Problem

The problem at hand is to find the non-overlapping occurrences of N repeated K characters. In simple terms we will be given a string and in that string there will be some repeated characters so we have to find out the appearances of specific characters and show the count for it. Here N is any character in the given input string and K is the count of character N.

Logic for The Above Problem

To solve this problem we will be using a loop to traverse over the given string and then we will check the values of N and K. And check for the substring which is having N, K times. And using an object to store the count of these occurrences.

Algorithm

  • Step 1 − First define a function called non_overlapping() and inside this function we will pass three parameters as n, k and the_str. Here n is the character, k is the character count for n and the_str is the input string

  • Step 2 − Initiate a variable called result which will be used to keep track of the count of non-overlapping occurrences.

  • Step 3 − Next, we will split the given string the_str with the help of delimiter 'B'. This will create a list of substrings.

  • Step 4 − A loop will be initiated over each substring in the list.

  • Step 5 − And for every substring we will filter out the occurrences of the character N using the filter function inside the lambda function. Here the lambda function will be used to check that each character is equal to K.

  • Step 6 − Now convert the filtered result in the list and then calculate the length.

  • Step 7 − Then we will divide the length by K to check the count of non-overlapping occurrences in the current substring.

  • Step 8 − After that add the value of count in the result variable.

  • Step 9 − When the iteration process will get over the final count of non-overlapping occurrences will be saved in the result variable.

Example

# Define the function
def non_overlapping(n, k, the_str):
   # Initialize the result object
   result = 0
   # Traverse the string
   for sub in the_str.split('B'):
      result += len(list(filter(lambda x: x == k, sub))) // n
   return result

the_str = 'AABBCCDDAABBGGHH'
K = "A"
N = 2  
print("The Input String is : " + str(the_str))
Output = non_overlapping(N, K, the_str)
print("Non-Overlapping occurrences result : " + str(Output))

Output

The Input String is : AABBCCDDAABBGGHH
Non-Overlapping occurrences result : 2

Complexity

The time complexity for finding the non-overlapping occurrences of N repeated K characters is O(m), here m is the length of the given input string. For each iteration we have filtered out the occurrences of character A and count how many times A has appeared K = 2 times.

Conclusion

As we have implemented the solution for finding the non-overlapping occurrences of N repeated K characters using Python. We have used a very straightforward method to solve this problem with the time complexity of O(m).

Updated on: 17-Oct-2023

42 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements