-
-
Save dellsystem/7077313 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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