Similar to the palindrome problem.
Problem:
Check if a String is an Isogram – You need to check if a given string is an isogram, considering only alphabetical characters and ignoring case sensitivity. This problem involves checking for duplicate characters within a string, similar to how you check for palindrome characters within a string in the original palindrome problem.
Code:
import java.util.HashSet;
import java.util.Set;
public class IsogramChecker {
public static boolean isIsogram(String str) {
// Convert the string to lowercase to ignore case sensitivity
str = str.toLowerCase();
// Use a set to keep track of seen characters
Set<Character> seen = new HashSet<>();
for (char ch : str.toCharArray()) {
// If the character is not alphabetical, ignore it
if (!Character.isLetter(ch)) {
continue;
}
// If the character is already in the set, it's not an isogram
if (seen.contains(ch)) {
return false;
}
// Otherwise, add it to the set
seen.add(ch);
}
// If we've checked all characters and haven't found duplicates, it's an isogram
return true;
}
public static void main(String[] args) {
String str1 = "abcdef";
String str2 = "programming";
String str3 = "hello";
System.out.println("Is str1 an isogram? " + isIsogram(str1)); // Output: true
System.out.println("Is str2 an isogram? " + isIsogram(str2)); // Output: false
System.out.println("Is str3 an isogram? " + isIsogram(str3)); // Output: false
}
}