-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRemoveDoubleChar.java
More file actions
35 lines (28 loc) · 899 Bytes
/
RemoveDoubleChar.java
File metadata and controls
35 lines (28 loc) · 899 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
class removeDoubleChar {
public static void main(String[] args) {
System.out.println("");
System.out.println("ll nn ss ee");
System.out.println("");
System.out.println("Nashvie, Tee");
System.out.println("");
String input = "Nashville, Tennessee";
String result = removeDoubleCharacters(input);
System.out.println(result);
}
public static String removeDoubleCharacters(String str) {
if (str.isEmpty()) {
return str;
}
StringBuilder result = new StringBuilder();
char prev = str.charAt(0);
result.append(prev);
for (int i = 1; i < str.length(); i++) {
char cur = str.charAt(i);
if (prev != cur) {
result.append(str.charAt(i));
}
prev = cur;
}
return result.toString();
}
}