-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeleteWords.java
49 lines (39 loc) · 1.23 KB
/
DeleteWords.java
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
49
package Stack;
import java.util.Vector;
public class DeleteWords {
static int removeConsecutiveSame(Vector <String > v)
{
int n = v.size();
// Start traversing the sequence
for (int i=0; i<n-1; )
{
// Compare the current string with next one
// Erase both if equal
if (v.get(i).equals(v.get(i+1)))
{
// Erase function delete the element and
// also shifts other element that's why
// i is not updated
v.remove(i);
v.remove(i);
// Update i, as to check from previous
// element again
if (i > 0)
i--;
// Reduce sequence size
n = n-2;
}
// Increment i, if not equal
else
i++;
}
// Return modified size
return v.size();
}
public static void main(String[] args) {
Vector<String> v = new Vector<>();
v.addElement("tom"); v.addElement("jerry");
v.addElement("jerry");v.addElement("tom");
System.out.println(removeConsecutiveSame(v));
}
}