Home > Back-end >  Finding Isograms
Finding Isograms

Time:12-10

I'm trying to find Isograms (words that do not contain multiples of any letter). Can't figure out why my code returns false for all cases

public class Main {

public static void main(String[] args) {

    System.out.println(Isogram.isIsogram("Dermatoglyphics"));
    System.out.println(Isogram.isIsogram("isogram"));
    System.out.println(Isogram.isIsogram("isIsogram"));
    System.out.println(Isogram.isIsogram("aba"));
    System.out.println(Isogram.isIsogram("moOse"));
    System.out.println(Isogram.isIsogram("thumbscrewjapingly"));



import java.util.Arrays;

public class Isogram {


public static boolean isIsogram(String str) {

    str = str.toLowerCase();
    char[] input = str.toCharArray();
    Arrays.sort(input);
    System.out.println(Arrays.toString(input));

    for (int i = 0; i < input.length - 1; i  ) {
        if (input[i] == input[i   1]);
        return false;
    }
    return true;
}

}

CodePudding user response:

if (input[i] == input[i   1]);
        return false;

the ; after the if statement implies that the if statement has an empty body. your return false; is outside of the if statement.

instead, do it like this:

if (input[i] == input[i   1]) return false;
  • Related