-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathRemove2ConsecutiveLetter.java
More file actions
32 lines (29 loc) · 889 Bytes
/
Copy pathRemove2ConsecutiveLetter.java
File metadata and controls
32 lines (29 loc) · 889 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
* You are given a string S consisting of lowercase letters. Your task is to remove all the consecutive duplicates from
* the string and output the result. For example, if the input string is "abbcddeff", the output should be "abcdef".
*
* Input: abbcddeff
* Output: abcdef
*/
import java.util.*;
public class Remove2ConsecutiveLetter
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
String result = removeConsecutiveDuplicates(s);
System.out.println(result);
}
private static String removeConsecutiveDuplicates(String s) {
String str = "";
char ch = '\0';
for (int i = 0; i < s.length(); i++) {
if(s.charAt(i)!=ch)
{
ch = s.charAt(i);
str += ch;
}
}
return str;
}
}