Remove adjacent duplicate characters - in JAVA Langauge

 Remove adjacent duplicate characters - in JAVA Langauge 
Given a string, write a program to recursively remove adjacent duplicate characters from string. The output string should not have any adjacent duplicates. See following examples.

Input:  azxxzy
Output: ay
[Hint: First "azxxzy" is reduced to "azzy". The string "azzy" contains duplicates, so it is further reduced to "ay"].

Input: caaabbbaacdddd
Output: -1

Input: acaaabbbacdddd
Output: acac

Input Format
Input contains a single string . See the sample input section for details.

Business Rules
       Input string should not contain any special characters or whitespace. Display "Invalid Input" to the screen if voilates any of these constraints.
       If the output string doesn't contain any character after removing the adjacent duplicate characters from the string, then it should print -1.
          Maximum length of the string should be 50. Else display "Size of the string is too large".

Output Format
Print the ouput string after  removing the adjacent duplicate characters from the input string.

Sample Input 1:
caaabbbaacdddd
Sample Output 1
acac

Sample Input 2:
caaabbbaacdddd
Sample Output 2:
-1

Sample Input 3:
abcd@
Sample Output 3:
Invalid Input

Sample Input 4:
youaregivenastringswriteaprogramtofindthelengthofthestring
Sample Output 4:
Size of the string is too large

Image result for java coding


Note :
Function specification is the following for C#,Java and C++.
Name:- RemoveDuplicateChars
Return Type:- string
Parameter(s):- string input1

public class RemoveDuplicateChars {
public static void main(String[] args) {
String input = args[0];
System.out.println(input + "-" + input.length());
StringBuffer buffer = new StringBuffer(input);RemoveDuplicateChars(buffer, 0, input.length());
System.out.println(buffer.toString() + "-" + buffer.length());
}
static void RemoveDuplicateChars(StringBuffer str, int start, int length) {
if (start == length)
return;
RemoveDuplicateChars(str, start + 1, length);
if (start > 0 && start != str.length() && str.charAt(start - 1) == str.charAt(start))
str.delete(start - 1, start + 1);
}
}

No comments