-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathW2.java
More file actions
48 lines (39 loc) · 984 Bytes
/
W2.java
File metadata and controls
48 lines (39 loc) · 984 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
Given a string s of n lowercase english letters, returns a stringg with no instances of three identical consecutive letters, obtained from s by deleting the minimum possible number of letters.
*/
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
System.out.println(Main.solution(str));
}
public static String solution(String s){
String res = "";
int count=1;
char[] arr = s.toCharArray();
for(int i=0;i<arr.length-1;i++){
if(arr[i]==arr[i+1]){
count++;
}
else{
count=1;
}
if(count>2){
arr[i-1]='@';
}
}
for(int i=0;i<arr.length;i++){
if(arr[i]!='@'){
res+=arr[i];
}
}
return res;
}
}
/*
OUTPUT:
s = "eedaaad" -----> = "eedaad"
s = "uuuuxaaaaxuuu" ------> = "uuxaaxuu"
*/