- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a string s and a number k, we have to find the number of k-length substrings of s, that occur more than once in s.

So, if the input is like s = "xxxyyy", k = 2, then the output will be 2

To solve this, we will follow these steps −

- seen := a new list
- for i in range 0 to size of s - k, do
- t := substring of s [from index i to i + k - 1]
- insert t at the end of seen

- mp := a map for all distinct element in seen and their occurrences
- return sum of all occurrences of each element in mp where occurrence is more than 1

Let us see the following implementation to get better understanding −

class Solution: def solve(self, s, k): from collections import Counter seen = [] for i in range(len(s) - k + 1): t = s[i : i + k] seen.append(t) s = Counter(seen) return sum(1 for x in s.values() if x > 1) ob = Solution() print(ob.solve("xxxyyy",2))

"xxxyyy",2

2

- Related Questions & Answers
- Array elements that appear more than once?
- Array elements that appear more than once in C?
- Program to find minimum string size that contains given substring in Python
- Program to find which element occurs exactly once in Python
- Program to find length of longest substring with character count of at least k in Python
- Write a Python program to remove a certain length substring from a given string
- Find the longest substring with k unique characters in a given string in Python
- MySQL Select where value exists more than once
- How to find the longest common substring from more than two strings in Python?
- Program to count subsets that sum up to k in python
- Program to find length of longest substring which contains k distinct characters in Python
- Find if there is a path of more than k length from a source in Python
- MySQL query to find a value appearing more than once?
- Check if the frequency of any character is more than half the length of the string in Python
- Python - Find words greater than given length

Advertisements