text: Contiguous subsegments of a string having distinct subsequent characters | Java implementation of the approach ; Function that prints the segments ; New array for every iteration ; Check if the character is in the array ; Driver code
code: 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 ) ; } }