Contiguous subsegments of a string having distinct subsequent characters
Last Updated :
20 Dec, 2022
Given string str of length L and an integer N, the task is to form a total of (L / N) contiguous sub-segments of the string which contain distinct subsequent characters.
Note: that the integer N will be a factor of the length of the string i.e L.
Examples:
Input: str = “geeksforgeeksgfg”, N = 4
Output:
gek
sfor
gek
sgf
The length of “geeksforgeeksgfg” is 16, therefore there will be 4 subsegments.
The first subsegment will contain the characters g, e, e and k but the alphabet ‘e’ is repeated,
So we discard one ‘e’. Therefore, the final subsegment will be “gek”.
Similarly, the other three subsegments will be “sfor”, “gek” and “sgf”.
Each subsegment should have subsequent distinct characters.
Input: str = “aabdekfgf”, N = 3
Output:
ab
dek
fg
Approach: An array is created every time the iteration is started over the new sub segment. Those elements are stored in a sequence in that array. If any element already exists in the array, then it is not pushed into the array.
Below is the implementation of the above approach:
CPP
#include<bits/stdc++.h>
using namespace std;
void sub_segments(string str, int n)
{
int l = str.length();
for ( int x = 0; x < l; x += n)
{
string newlist = str.substr(x, n);
list< char > arr;
list< char >::iterator it;
for ( auto y:newlist)
{
it = find(arr.begin(), arr.end(), y);
if (it == arr.end())
arr.push_back(y);
}
for ( auto y:arr)
cout << y;
cout << endl;
}
}
int main()
{
string str = "geeksforgeeksgfg" ;
int n = 4;
sub_segments(str, n);
}
|
Java
import java.util.*;
class GFG
{
static void sub_segments(String str, int n)
{
int l = str.length();
for ( int x = 0 ; x < l; x += n)
{
String newlist = str.substring(x, x + n);
List<Character> arr = new ArrayList<Character>();
for ( char y : newlist.toCharArray())
{
if (!arr.contains(y))
arr.add(y);
}
for ( char y : arr)
System.out.print(y);
System.out.println();
}
}
public static void main(String[] args)
{
String str = "geeksforgeeksgfg" ;
int n = 4 ;
sub_segments(str, n);
}
}
|
Python3
def sub_segments (string, n):
l = len (string)
for x in range ( 0 , l, n):
newlist = string[x : x + n]
arr = []
for y in newlist:
if y not in arr:
arr.append (y)
print (''.join (arr))
string = "geeksforgeeksgfg"
n = 4
sub_segments (string, n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void sub_segments(String str, int n)
{
int l = str.Length;
for ( int x = 0; x < l; x += n)
{
String newlist = str.Substring(x, n);
List< char > arr = new List< char >();
foreach ( char y in newlist.ToCharArray())
{
if (!arr.Contains(y))
arr.Add(y);
}
foreach ( char y in arr)
Console.Write(y);
Console.WriteLine();
}
}
public static void Main(String[] args)
{
String str = "geeksforgeeksgfg" ;
int n = 4;
sub_segments(str, n);
}
}
|
Javascript
<script>
function sub_segments(str, n) {
let l = str.length;
for (let x = 0; x < l; x += n) {
let newlist = str.substr(x, n);
let arr = [];
for (let y of newlist) {
if (!arr.includes(y))
arr.push(y);
}
for (let y of arr)
document.write(y);
document.write( "<br>" );
}
}
let str = "geeksforgeeksgfg" ;
let n = 4;
sub_segments(str, n);
</script>
|
Time Complexity: O(L), Here L is the length of the string.
Auxiliary Space: O(n), The extra space is used to store the substrings.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...