The naming of the class is OK, I think. It could be better named AdjacentSameCharacterPairsRemover
. "Same" is shorter than "duplicate". "Pairs" corrects a misunderstanding that I initially had, which was that "aaa"
→ ""
instead of "aaa"
→ "a"
. The name still wouldn't convey the idea that the elimination is applied repeatedly, but there's only so much information that you can fit into a name.
I don't think that the function name needs to be that long, if the class name is already descriptive. The parameter name, candidate
, is peculiar. What office is that string running for, or what validation are you performing on it?
I don't recommend marking parameters and local variables as final
— it just adds visual clutter. Functions should be short enough that you can easily keep track of all the variables in your head simultaneously.
You don't need the special case for if (candidate.length() < 2)
; the algorithm works just fine without it. The added complexity isn't worth the potential performance benefit.
I would work directly with the char[]
and dispense with the Stack
and StringBuilder
. By doing so, you would eliminate the need for extra storage, the boxing of char
s as Character
s, the synchronized
behaviour of java.util.Stack
, and the semi-deprecated status of java.util.Stack
. You also would't need to push all of the original characters onto the stack and then filter it, since the character array is the input. You wouldn't need to translate the Stack
back to a StringBuilder
to make the String
. The code would be simplified so much that you could eliminate the helper function altogether.
public class AdjacentSameCharacterPairsRemover {
// Suppress the default constructor
private AdjacentSameCharacterPairsRemover() {}
public static String process(String s) {
char[] chars = s.toCharArray();
int len = 0; // Length of result (initially empty)
for (int i = 0; i < chars.length; i++) {
if (len == 0 || chars[i] != chars[len - 1]) {
chars[len++] = chars[i]; // Push one char onto "stack"
} else {
len--; // Pop one char from "stack"
}
}
return new String(chars, 0, len);
}
}
The naming of the class is OK, I think. It could be better named AdjacentSameCharacterPairsRemover
. "Same" is shorter than "duplicate". "Pairs" corrects a misunderstanding that I initially had, which was that "aaa"
→ ""
instead of "aaa"
→ "a"
. The name still wouldn't convey the idea that the elimination is applied repeatedly, but there's only so much information that you can fit into a name.
I don't think that the function name needs to be that long, if the class name is already descriptive. The parameter name, candidate
, is peculiar. What office is that string running for, or what validation are you performing on it?
I don't recommend marking parameters and local variables as final
— it just adds visual clutter. Functions should be short enough that you can easily keep track of all the variables in your head simultaneously.
You don't need the special case for if (candidate.length() < 2)
; the algorithm works just fine without it. The added complexity isn't worth the potential performance benefit.
I would work directly with the char[]
and dispense with the Stack
and StringBuilder
. By doing so, you would eliminate the need for extra storage, the boxing of char
s as Character
s, the synchronized
behaviour of java.util.Stack
, and the semi-deprecated status of java.util.Stack
. You also would't need to push all of the original characters onto the stack and then filter it, since the character array is the input. The code would be simplified so much that you could eliminate the helper function altogether.
public class AdjacentSameCharacterPairsRemover {
// Suppress the default constructor
private AdjacentSameCharacterPairsRemover() {}
public static String process(String s) {
char[] chars = s.toCharArray();
int len = 0; // Length of result (initially empty)
for (int i = 0; i < chars.length; i++) {
if (len == 0 || chars[i] != chars[len - 1]) {
chars[len++] = chars[i]; // Push one char onto "stack"
} else {
len--; // Pop one char from "stack"
}
}
return new String(chars, 0, len);
}
}
The naming of the class is OK, I think. It could be better named AdjacentSameCharacterPairsRemover
. "Same" is shorter than "duplicate". "Pairs" corrects a misunderstanding that I initially had, which was that "aaa"
→ ""
instead of "aaa"
→ "a"
. The name still wouldn't convey the idea that the elimination is applied repeatedly, but there's only so much information that you can fit into a name.
I don't think that the function name needs to be that long, if the class name is already descriptive. The parameter name, candidate
, is peculiar. What office is that string running for, or what validation are you performing on it?
I don't recommend marking parameters and local variables as final
— it just adds visual clutter. Functions should be short enough that you can easily keep track of all the variables in your head simultaneously.
You don't need the special case for if (candidate.length() < 2)
; the algorithm works just fine without it. The added complexity isn't worth the potential performance benefit.
I would work directly with the char[]
and dispense with the Stack
and StringBuilder
. By doing so, you would eliminate the need for extra storage, the boxing of char
s as Character
s, the synchronized
behaviour of java.util.Stack
, and the semi-deprecated status of java.util.Stack
. You also would't need to push all of the original characters onto the stack and then filter it, since the character array is the input. You wouldn't need to translate the Stack
back to a StringBuilder
to make the String
. The code would be simplified so much that you could eliminate the helper function altogether.
public class AdjacentSameCharacterPairsRemover {
// Suppress the default constructor
private AdjacentSameCharacterPairsRemover() {}
public static String process(String s) {
char[] chars = s.toCharArray();
int len = 0; // Length of result (initially empty)
for (int i = 0; i < chars.length; i++) {
if (len == 0 || chars[i] != chars[len - 1]) {
chars[len++] = chars[i]; // Push one char onto "stack"
} else {
len--; // Pop one char from "stack"
}
}
return new String(chars, 0, len);
}
}
The naming of the class is OK, I think. It could be better named AdjacentSameCharacterPairsRemover
. "Same" is shorter than "duplicate". "Pairs" corrects a misunderstanding that I initially had, which was that "aaa"
→ ""
instead of "aaa"
→ "a"
. The name still wouldn't convey the idea that the elimination is applied repeatedly, but there's only so much information that you can fit into a name.
I don't think that the function name needs to be that long, if the class name is already descriptive. The parameter name, candidate
, is peculiar. What office is that string running for, or what validation are you performing on it?
I don't recommend marking parameters and local variables as final
— it just adds visual clutter. Functions should be short enough that you can easily keep track of all the variables in your head simultaneously.
You don't need the special case for if (candidate.length() < 2)
; the algorithm works just fine without it. The added complexity isn't worth the potential performance benefit.
I would work directly with the char[]
and dispense with the Stack
and StringBuilder
. By doing so, you would eliminate the need for extra storage, the boxing of char
s as Character
s, the synchronized
behaviour of java.util.Stack
, and the semi-deprecated status of java.util.Stack
. You also would't need to push all of the original characters onto the stack and then filter it, since the character array is the input. The code would be simplified so much that you could eliminate the helper function altogether.
public class AdjacentSameCharacterPairsRemover {
// Suppress the default constructor
private AdjacentSameCharacterPairsRemover() {}
public static String process(String s) {
char[] chars = s.toCharArray();
int stackToplen = -1;0; // Index of topLength of stack. Stackresult starts(initially empty.)
for (int i = 0; i < chars.length; i++) {
if (stackToplen <== 0 || chars[i] != chars[stackTop]chars[len - 1]) {
chars[++stackTop]chars[len++] = chars[i]; // Push one char onto stack"stack"
} else {
stackToplen--; // Pop one char from stack"stack"
}
}
return new String(chars, 0, stackTop + 1len);
}
}
The naming of the class is OK, I think. It could be better named AdjacentSameCharacterPairsRemover
. "Same" is shorter than "duplicate". "Pairs" corrects a misunderstanding that I initially had, which was that "aaa"
→ ""
instead of "aaa"
→ "a"
. The name still wouldn't convey the idea that the elimination is applied repeatedly, but there's only so much information that you can fit into a name.
I don't think that the function name needs to be that long, if the class name is already descriptive. The parameter name, candidate
, is peculiar. What office is that string running for, or what validation are you performing on it?
I don't recommend marking parameters and local variables as final
— it just adds visual clutter. Functions should be short enough that you can easily keep track of all the variables in your head simultaneously.
You don't need the special case for if (candidate.length() < 2)
; the algorithm works just fine without it. The added complexity isn't worth the potential performance benefit.
I would work directly with the char[]
and dispense with the Stack
and StringBuilder
. By doing so, you would eliminate the need for extra storage, the boxing of char
s as Character
s, the synchronized
behaviour of java.util.Stack
, and the semi-deprecated status of java.util.Stack
. You also would't need to push all of the original characters onto the stack and then filter it, since the character array is the input. The code would be simplified so much that you could eliminate the helper function altogether.
public class AdjacentSameCharacterPairsRemover {
// Suppress the default constructor
private AdjacentSameCharacterPairsRemover() {}
public static String process(String s) {
char[] chars = s.toCharArray();
int stackTop = -1; // Index of top of stack. Stack starts empty.
for (int i = 0; i < chars.length; i++) {
if (stackTop < 0 || chars[i] != chars[stackTop]) {
chars[++stackTop] = chars[i]; // Push one char onto stack
} else {
stackTop--; // Pop one char from stack
}
}
return new String(chars, 0, stackTop + 1);
}
}
The naming of the class is OK, I think. It could be better named AdjacentSameCharacterPairsRemover
. "Same" is shorter than "duplicate". "Pairs" corrects a misunderstanding that I initially had, which was that "aaa"
→ ""
instead of "aaa"
→ "a"
. The name still wouldn't convey the idea that the elimination is applied repeatedly, but there's only so much information that you can fit into a name.
I don't think that the function name needs to be that long, if the class name is already descriptive. The parameter name, candidate
, is peculiar. What office is that string running for, or what validation are you performing on it?
I don't recommend marking parameters and local variables as final
— it just adds visual clutter. Functions should be short enough that you can easily keep track of all the variables in your head simultaneously.
You don't need the special case for if (candidate.length() < 2)
; the algorithm works just fine without it. The added complexity isn't worth the potential performance benefit.
I would work directly with the char[]
and dispense with the Stack
and StringBuilder
. By doing so, you would eliminate the need for extra storage, the boxing of char
s as Character
s, the synchronized
behaviour of java.util.Stack
, and the semi-deprecated status of java.util.Stack
. You also would't need to push all of the original characters onto the stack and then filter it, since the character array is the input. The code would be simplified so much that you could eliminate the helper function altogether.
public class AdjacentSameCharacterPairsRemover {
// Suppress the default constructor
private AdjacentSameCharacterPairsRemover() {}
public static String process(String s) {
char[] chars = s.toCharArray();
int len = 0; // Length of result (initially empty)
for (int i = 0; i < chars.length; i++) {
if (len == 0 || chars[i] != chars[len - 1]) {
chars[len++] = chars[i]; // Push one char onto "stack"
} else {
len--; // Pop one char from "stack"
}
}
return new String(chars, 0, len);
}
}
The naming of the class is OK, I think. It could be better named AdjacentSameCharacterPairsRemover
. "Same" is shorter than "duplicate". "Pairs" corrects a misunderstanding that I initially had, which was that "aaa"
→ ""
instead of "aaa"
→ "a"
. The name still wouldn't convey the idea that the elimination is applied repeatedly, but there's only so much information that you can fit into a name.
I don't think that the function name needs to be that long, if the class name is already descriptive. The parameter name, candidate
, is peculiar. What office is that string running for, or what validation are you performing on it?
I don't recommend marking parameters and local variables as final
— it just adds visual clutter. Functions should be short enough that you can easily keep track of all the variables in your head simultaneously.
You don't need the special case for if (candidate.length() < 2)
; the algorithm works just fine without it. The added complexity isn't worth the potential performance benefit.
I would work directly with the char[]
and dispense with the Stack
and StringBuilder
. By doing so, you would eliminate the need for extra storage, the boxing of char
s as Character
s, the synchronized
behaviour of java.util.Stack
, and the semi-deprecated status of java.util.Stack
. You also would't need to push all of the original characters onto the stack and then filter it, since the character array is the input. The code would be simplified so much that you could eliminate the helper function altogether.
public class AdjacentSameCharacterPairsRemover {
// Suppress the default constructor
private AdjacentSameCharacterPairsRemover() {}
public static String process(String s) {
char[] chars = s.toCharArray();
int stackTop = -1; // Index of top of stack. Stack starts empty.
for (int i = 0; i < chars.length; i++) {
if (stackTop < 0 || chars[i] != chars[stackTop]) {
chars[++stackTop] = chars[i]; // Push one char onto stack
} else {
stackTop--; // Pop one char from stack
}
}
return new String(chars, 0, stackTop + 1);
}
}
The naming of the class is OK, I think. It could be better named AdjacentSameCharacterPairsRemover
. "Same" is shorter than "duplicate". "Pairs" corrects a misunderstanding that I initially had, which was that "aaa"
→ ""
instead of "aaa"
→ "a"
. The name still wouldn't convey the idea that the elimination is applied repeatedly, but there's only so much information that you can fit into a name.
I don't think that the function name needs to be that long, if the class name is already descriptive.
I don't recommend marking parameters and local variables as final
— it just adds visual clutter. Functions should be short enough that you can easily keep track of all the variables in your head simultaneously.
I would work directly with the char[]
and dispense with the Stack
and StringBuilder
. By doing so, you would eliminate the need for extra storage, the boxing of char
s as Character
s, the synchronized
behaviour of java.util.Stack
, and the semi-deprecated status of java.util.Stack
. You also would't need to push all of the original characters onto the stack and then filter it, since the character array is the input. The code would be simplified so much that you could eliminate the helper function altogether.
public class AdjacentSameCharacterPairsRemover {
// Suppress the default constructor
private AdjacentSameCharacterPairsRemover() {}
public static String process(String s) {
char[] chars = s.toCharArray();
int stackTop = -1; // Index of top of stack. Stack starts empty.
for (int i = 0; i < chars.length; i++) {
if (stackTop < 0 || chars[i] != chars[stackTop]) {
chars[++stackTop] = chars[i]; // Push one char onto stack
} else {
stackTop--; // Pop one char from stack
}
}
return new String(chars, 0, stackTop + 1);
}
}
The naming of the class is OK, I think. It could be better named AdjacentSameCharacterPairsRemover
. "Same" is shorter than "duplicate". "Pairs" corrects a misunderstanding that I initially had, which was that "aaa"
→ ""
instead of "aaa"
→ "a"
. The name still wouldn't convey the idea that the elimination is applied repeatedly, but there's only so much information that you can fit into a name.
I don't think that the function name needs to be that long, if the class name is already descriptive. The parameter name, candidate
, is peculiar. What office is that string running for, or what validation are you performing on it?
I don't recommend marking parameters and local variables as final
— it just adds visual clutter. Functions should be short enough that you can easily keep track of all the variables in your head simultaneously.
You don't need the special case for if (candidate.length() < 2)
; the algorithm works just fine without it. The added complexity isn't worth the potential performance benefit.
I would work directly with the char[]
and dispense with the Stack
and StringBuilder
. By doing so, you would eliminate the need for extra storage, the boxing of char
s as Character
s, the synchronized
behaviour of java.util.Stack
, and the semi-deprecated status of java.util.Stack
. You also would't need to push all of the original characters onto the stack and then filter it, since the character array is the input. The code would be simplified so much that you could eliminate the helper function altogether.
public class AdjacentSameCharacterPairsRemover {
// Suppress the default constructor
private AdjacentSameCharacterPairsRemover() {}
public static String process(String s) {
char[] chars = s.toCharArray();
int stackTop = -1; // Index of top of stack. Stack starts empty.
for (int i = 0; i < chars.length; i++) {
if (stackTop < 0 || chars[i] != chars[stackTop]) {
chars[++stackTop] = chars[i]; // Push one char onto stack
} else {
stackTop--; // Pop one char from stack
}
}
return new String(chars, 0, stackTop + 1);
}
}