WAP to Remove Consecutive Duplicate Characters from String

This Java program aims to remove consecutive duplicate characters from a given string. It utilizes a stack data structure to achieve this.

Remove Consecutive Duplicate Characters from String

package com.softwaretestingo.interviewprograms;
import java.util.Stack;
public class InterviewPrograms39 
{
	/*
	 * Input string : weelccoommee hhoommeee 
	 * Output string : welcome home
	 */
	public static void main(String[] args) 
	{
		String s = "weelccoommee hhoommeee";
		Stack<Character> st = new Stack<>();
		st.push(s.charAt(0));
		for (int i = 1; i<s.length(); i++ ) 
		{
			if (s.charAt(i-1) != s.charAt(i))
				st.push(s.charAt(i));
		}
		for ( Character c : st )
			System.out.print(c) ;
	}
}

Output

welcome home

Here’s a breakdown of the program:

  1. Define the main method:
    • The entry point of the program is the main method.
  2. Initialize the input string and create a stack:
    • The string s is initialized with the value “weelccoommee hhoommeee”.
    • A stack of characters named st is created.
  3. Process the string and remove consecutive duplicates:
    • The program iterates through each character of the string using a for loop, starting from the second character.
    • It compares the current character with the previous character using the condition s.charAt(i-1) != s.charAt(i).
    • If the current character is different from the previous character, it is pushed onto the stack using st.push(s.charAt(i)).
  4. Print the non-duplicate characters:
    • The program then uses an enhanced for loop to iterate over the characters in the stack.
    • Each character is printed using System.out.print(c).

The program outputs the non-duplicate characters from the original string. In this case, the output will be “welcome home”, where consecutive duplicate characters have been removed.

Alternative 1:

This Java program aims to remove consecutive duplicate characters from a given input string using regular expressions.

package com.softwaretestingo.interviewprograms;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class InterviewPrograms39_1 
{
	/*
	 * Input string : weelccoommee hhoommeee 
	 * Output string : welcome home
	 */
	public static void main(String[] args) 
	{
		String s = " weelccoomme hhommeee " ;
		Pattern pattern = Pattern.compile("(.)\\1*");
		Matcher m= pattern.matcher ( s ) ;
		String output = "";
		while ( m.find())
		{
			output= output + m.group().charAt(0);
		}
		System.out.println ( output ) ;
	}
}

Output

welcome home

Here’s a breakdown of the program:

  1. Define the main method:
    • The entry point of the program is the main method.
  2. Initialize the input string and compile the regex pattern:
    • The string s is initialized with the value ” weelccoomme hhommeee “.
    • A regex pattern is created using Pattern.compile(“(.)\\1*”). This pattern looks for consecutive repeated characters.
  3. Create a Matcher object and find matches:
    • A Matcher object named m is created by applying the pattern on the input string s.
    • The m.find() method is used to find occurrences of the pattern in the input string.
  4. Build the output string:
    • While the m.find() method continues to find matches, the program extracts the matched group using m.group().
    • The first character of each matched group is extracted using charAt(0) and added to the output string.
  5. Print the output:
    • The program prints the final output string, which contains the non-duplicate characters from the original string.

The program outputs “welcome home”, where consecutive duplicate characters have been removed using regular expressions.

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment