Check If a String Contains All Binary Codes of Size K code example
Example 1: how to check if a string is in alphabetical order in java
public static boolean checkAlphabetic(String input) {
for (int i = 0; i != input.length(); ++i) {
if (!Character.isLetter(input.charAt(i))) {
return false;
}
}
return true;
}
Example 2: how to check if a string contains only alphabets and space in java
class IsAlpha
{
public static boolean isAlpha(String s) {
if (s == null) {
return false;
}
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (!(c >= 'A' && c <= 'Z') && !(c >= 'a' && c <= 'z')) {
return false;
}
}
return true;
}
public static void main(String[] args)
{
String s = "ABCD";
System.out.println("IsAlpha: " + isAlpha(s));
}
}
Example 3: how to check if a string contains only alphabets and space in java
class IsAlpha
{
public static boolean isAlpha(String s) {
return s != null && s.chars().allMatch(Character::isLetter);
}
public static void main(String[] args)
{
String s = "ABCD";
System.out.println("IsAlpha: " + isAlpha(s));
}
}
Example 4: how to check if an arraylist contains a value in java recursion
public static boolean contains(ArrayList<Integer> list, int value) {
return contains(list, value, 0);
}
private static boolean contains(ArrayList<Integer> list, int value, int idx) {
boolean hasInt = false;
if (idx < list.size()) {
if (list.get(idx) == value) {
hasInt = true;
} else {
hasInt = contains(list, value, idx + 1);
}
}
return hasInt;
}