-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDEQUEDELETESAMEFROMBOTHENDS.java
More file actions
41 lines (41 loc) · 1.42 KB
/
DEQUEDELETESAMEFROMBOTHENDS.java
File metadata and controls
41 lines (41 loc) · 1.42 KB
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
class Solution {
// public int minimumLength(String s) {
// Deque<Character> dq = new ArrayDeque<>();
// int startPointer = 0;
// int endPointer = s.length() - 1;
// for (char c : s.toCharArray()) {
// dq.addLast(c);
// }
// while (dq.size() > 1 && startPointer < endPointer) {
// if (dq.isEmpty()) break;
// char prefixFirst = dq.peekFirst();
// char suffixFirst = dq.peekLast();
// if (prefixFirst != suffixFirst) break;
// while (!dq.isEmpty() && dq.peekFirst() == prefixFirst) {
// // prefix.append(dq.removeFirst());
// dq.removeFirst();
// startPointer++;
// }
// while (!dq.isEmpty() && dq.peekLast() == suffixFirst) {
// // suffix.append(dq.removeLast());
// dq.removeLast();
// endPointer--;
// }
// }
// return dq.size();
// }
public int minimumLength(String str){
int left=0;
int right=str.length()-1;
while(left<right && str.charAt(left)==str.charAt(right)){
char ch=str.charAt(left);
while(left<=right && str.charAt(left)==ch){
left++;
}
while(left<=right && str.charAt(right)==ch){
right--;
}
}
return right-left+1;
}
}