VOOZH about

URL: https://www.geeksforgeeks.org/dsa/print-consecutive-characters-together-line/

⇱ Print consecutive characters together in a line - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Print consecutive characters together in a line

Last Updated : 25 Oct, 2022

Given a sequence of characters, print consecutive sequence of characters in a line, otherwise 
print it in a new line.

Examples: 

Input : ABCXYZACCD
Output : ABC
 XYZ
 A
 C
 CD

Input : ABCZYXACCD
Output: ABC
 ZYX
 A
 C
 CD

The idea is to traverse string from left to right. For every traversed character, print it in a line if it is consecutive to previous one, else print a new line character. 

Implementation:


Output
ABC
XYZ
A
C
CD

Time Complexity: O(n) 
Auxiliary Space: O(1)

Comment
Article Tags:
Article Tags: