Skip to content

Instantly share code, notes, and snippets.

@dellsystem
Forked from anonymous/Question1.java
Last active December 26, 2015 02:19
Show Gist options
  • Save dellsystem/7077313 to your computer and use it in GitHub Desktop.
Save dellsystem/7077313 to your computer and use it in GitHub Desktop.
public class Question1{
public static void main(String[]args){
String[] shouldBeTrue = {
"89012",
"xyZabc",
"123",
"DcbaZ",
};
String[] shouldBeFalse = {
"09283dfsdfD",
"abCba",
"1abC"
};
int i;
for (i = 0; i < shouldBeTrue.length; i++) {
System.out.println(isConsecutive(shouldBeTrue[i]) == true);
}
for (i = 0; i < shouldBeFalse.length; i++) {
System.out.println(isConsecutive(shouldBeFalse[i]) == false);
}
}
public static boolean isConsecutive(String s){
return isConsecutiveAscending(s) || isConsecutiveDescending(s);
}
public static boolean isConsecutiveAscending(String s){
s = s.toLowerCase();
for (int i = 0; i < s.length() - 1; i++) {
int thisChar = (int) s.charAt(i);
int nextChar = (int) s.charAt(i + 1);
if ((thisChar + 1 == nextChar) || (thisChar == 57 && nextChar == 48) ||
(thisChar == 122 && nextChar == 97)) {
continue;
} else {
return false;
}
}
return true;
}
public static boolean isConsecutiveDescending(String s){
s = s.toLowerCase();
for (int i = 0; i < s.length() - 1; i++) {
int thisChar = (int) s.charAt(i);
int nextChar = (int) s.charAt(i + 1);
if ((thisChar == nextChar + 1) || (thisChar == 48 && nextChar == 57) ||
(thisChar == 97 && nextChar == 122)) {
continue;
} else {
return false;
}
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment