Given a string, that contains special character together with alphabets (‘a’ to ‘z’ and ‘A’ to ‘Z’), reverse the string in a way that special characters are not affected.
Example: Input: str = "a:b!c" Output: str = "c:b!a"
I am using two indexes one to start from the beginning and the other from the end of the array.Swap if they are valid chars and then increment indexes,increment indexes if invalid chars.Any feedback appreciated!
P.S: I am using static methods for convenience.
The Code:
public class ReverseArrayWithoutSpecialChars {
static HashSet<Character> invalidChars = new HashSet<Character>(Arrays.asList(',', '?', ':','!','$'));
public static char[] reverseSkippingSpecialChars(char[] charArray) {
int l = 0;
int r = charArray.length - 1;
for (int i = 0; i < charArray.length; i++) {
if (r <= l) {
break;
}
if (!isInvalidChar(charArray[l]) && !isInvalidChar(charArray[r])) {
charArray = swapChars(charArray, l, r);
l++;
r--;
} else if (isInvalidChar(charArray[l])) {
l++;
} else if (isInvalidChar(charArray[r])) {
r--;
}
}
return charArray;
}
public static char[] swapChars(char[] charArray, int index1, int index2) {
char temp = charArray[index1];
charArray[index1] = charArray[index2];
charArray[index2] = temp;
return charArray;
}
public static boolean isInvalidChar(char character) {
return invalidChars.contains(character);
}
public static void main(String args[]) {
char[] charArray = new char[]{'a', 'b', 'c', 'd', '?', 'f', 's', ':', 'w'};
System.out.println(reverseSkippingSpecialChars(charArray));
}
}