Contiguous subsegments of a string having distinct subsequent characters - GeeksforGeeks

02 Jun, 2021
Given string str of length L and an integer N, the task is to form a total of (L / N) contiguous subsegments 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 dekfg 
Approach: An array is created every time the iteration is started over the new subsegment. 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
Java
Python3
C#
Javascript
// C++ implementation of the approach#include<bits/stdc++.h>using namespace std; // Function that prints the segmentsvoid sub_segments(string str, int n){    int l = str.length();    for (int x = 0; x < l; x += n)    {        string newlist = str.substr(x, n);                 // New array for every iteration        list<char> arr;         // Iterator for new array        list<char>::iterator it;         for(auto y:newlist)        {            it = find(arr.begin(), arr.end(), y);                         // Check if iterator points to end or not            if(it == arr.end())                arr.push_back(y);        }         for(auto y:arr)            cout << y;        cout << endl;    }} // Driver codeint main(){    string str = "geeksforgeeksgfg";    int n = 4;    sub_segments(str, n);} // This code is contributed by Sanjit_Prasad
// Java implementation of the approachimport java.util.*; class GFG{ // Function that prints the segmentsstatic 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);         // New array for every iteration        List<Character> arr = new ArrayList<Character>();        for (char y : newlist.toCharArray())        {             // Check if the character is in the array            if (!arr.contains(y))                arr.add(y);        }        for (char y : arr)            System.out.print(y);        System.out.println();    }} // Driver codepublic static void main(String[] args){    String str = "geeksforgeeksgfg";    int n = 4;    sub_segments(str, n);}} // This code is contributed by Rajput-Ji
# Python3 implementation of the approach # Function that prints the segmentsdef sub_segments (string, n):    l = len (string)    for x in range (0, l, n):        newlist = string[x : x + n]         # New array for every iteration        arr = []        for y in newlist:            # Check if the character is in the array            if y not in arr:                arr.append (y)                print (''.join (arr)) # Driver codestring = "geeksforgeeksgfg"n = 4sub_segments (string, n)
// C# implementation of the approachusing System;using System.Collections.Generic; class GFG{ // Function that prints the segmentsstatic 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);         // New array for every iteration        List<char> arr = new List<char>();        foreach (char y in newlist.ToCharArray())        {             // Check if the character is in the array            if (!arr.Contains(y))                arr.Add(y);        }        foreach (char y in arr)            Console.Write(y);        Console.WriteLine();    }} // Driver codepublic static void Main(String[] args){    String str = "geeksforgeeksgfg";    int n = 4;    sub_segments(str, n);}} // This code is contributed by Rajput-Ji
<script>// Javascript implementation of the approach  // Function that prints the segmentsfunction sub_segments(str, n) {    let l = str.length;    for (let x = 0; x < l; x += n) {        let newlist = str.substr(x, n);         // New array for every iteration        let arr = [];         for (let y of newlist) {             // Check if the character is in the array            if (!arr.includes(y))                arr.push(y);        }        for (let y of arr)            document.write(y);        document.write("<br>");    }} // Driver code let str = "geeksforgeeksgfg";let n = 4;sub_segments(str, n); // This code is contributed by gfgking </script>
gek
sfor
gek
sgf
 
Rajput-Ji
Sanjit_Prasad
gfgking
substring
Arrays
School Programming
Strings
Arrays
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Window Sliding Technique
Program to find sum of elements in a given array
Reversal algorithm for array rotation
Trapping Rain Water
Find duplicates in O(n) time and O(1) extra space | Set 1
Python Dictionary
Inheritance in C++
Reverse a string in Java
Interfaces in Java
C++ Classes and Objects