Distinct Substring frequency counter
Last Updated :
17 Apr, 2023
Given a string s of length N containing only lowercase English letters and an integer K, the task is to find all distinct substrings of S of length K, and for each substring, count the number of times it occurs in S. You need to return a list of all distinct substrings along with their frequency of occurrence in S.
Examples:
Input: S = “abcabcabc”, K = 3
Output: [(abc, 3), (bca, 2), (cab, 2)]
Input : s = “aabaaaba”, k = 2
Output : [(“aa”, 3), (“ab”, 2), (“ba”, 2)]
Approach: This can be solved by the following idea:
Using the Sliding window concept, where we will maintain two pointers one on k and another on 0 (0- indexed), getting the string formed between these two pointers. Adding the formed string in the map to maintain the frequency of substrings. Incrementing in these two pointers and repeating the process.
Steps involved in the implementation of code:
- Maintaining a map to store frequencies of substrings formed.
- Start iterating from i = 0.
- Get the string from s.substr(i, K). Insert it into the map.
- Increment i up to N – K -1.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void getAll(string s, int K)
{
unordered_map<string, int > m;
int i = 0;
while (i < s.length() - K + 1) {
string formed = s.substr(i, K);
m[formed]++;
i++;
}
for ( auto a : m) {
cout << '(' << a.first << ", " << a.second << ')'
<< endl;
}
}
int main()
{
string s = "abcabcabc" ;
int K = 3;
getAll(s, K);
return 0;
}
|
Java
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args)
{
String s = "abcabcabc" ;
int K = 3 ;
getAll(s, K);
}
public static void getAll(String s, int K)
{
Map<String, Integer> m = new HashMap<>();
int i = 0 ;
while (i < s.length() - K + 1 ) {
String formed = s.substring(i, i + K);
m.put(formed, m.getOrDefault(formed, 0 ) + 1 );
i++;
}
for (Map.Entry<String, Integer> entry :
m.entrySet()) {
System.out.println( "(" + entry.getKey() + ", "
+ entry.getValue() + ")" );
}
}
}
|
Python3
def get_all(s: str , K: int ):
m = {}
i = 0
while i < len (s) - K + 1 :
formed = s[i:i + K]
m[formed] = m.get(formed, 0 ) + 1
i + = 1
for key, value in m.items():
print (f "({key}, {value})" )
if __name__ = = '__main__' :
s = "abcabcabc"
K = 3
get_all(s, K)
|
Javascript
function getAll(s, K) {
const m = new Map();
let i = 0;
while (i < s.length - K + 1) {
const formed = s.substr(i, K);
m.set(formed, (m.get(formed) || 0) + 1);
i++;
}
for (const [key, value] of m) {
console.log(`(${key}, ${value})`);
}
}
const s = "abcabcabc" ;
const K = 3;
getAll(s, K);
|
C#
using System;
using System.Collections.Generic;
class MainClass {
static void GetAll( string s, int K) {
Dictionary< string , int > m = new Dictionary< string , int >();
int i = 0;
while (i < s.Length - K + 1) {
string formed = s.Substring(i, K);
if (m.ContainsKey(formed)) {
m[formed]++;
}
else {
m.Add(formed, 1);
}
i++;
}
foreach (KeyValuePair< string , int > entry in m) {
Console.WriteLine( "({0}, {1})" , entry.Key, entry.Value);
}
}
static void Main() {
string s = "abcabcabc" ;
int K = 3;
GetAll(s, K);
}
}
|
Output
(bca, 2)
(cab, 2)
(abc, 3)
Time complexity: O(N*logN)
Auxiliary space: O(N-K) As the map required storage equal to unique no. of substring in worst case it is O(N-K) and best case it is O(1)
Share your thoughts in the comments
Please Login to comment...