An N-Drome is a string that is the same forwards and backwards when split into n-character units. For example, a string would be a 1-drome if and only if it were a standard palindrome. An example of a 2-drome would be abcdab since its 2-character units are ab, cd, and ab, and its first and last 2-character units are identical.package ndrome;import java.util.Scanner;public class NDrome {    public static void main(String[] args) {        System.out.print(enter a string: );        Scanner in = new Scanner(System.in);        String original = in.nextLine();        System.out.print(enter the n value: );        int n = in.nextInt();        if(n-1>=original.length()){            System.out.println(Error:N value is greater than string length! exiting...);            System.exit(0);        }        int y = 2;        here: for (int i = 0, j = original.length() - 1;;) {            int x = n - 1;            String a = , b = ;            while (x >= 0) {                if (i >= original.length()) {                    break here;                } else {                    a = a + original.charAt(i);                    i++;                    b = b + original.charAt(j - x);                    x--;                }            }            j=j-n;            if (a.equals(b)) {                y = 1;            } else {                y = 0;                break;            }        }        if (y == 1) {            System.out.print(this is a  + n + -drome);        } else if (y == 0) {            System.out.print(this is not a  + n + -drome);        }    }}
Is my N-drome (variation of palindrome) checking program efficient?
java;optimization;palindrome
The good news is that your code appears to be correct.  The bad news is that it is very hard to follow.As @Jamal says, it is good practice to define a function rather than stuffing everything into main().  There are several reasons for that, including several reasons specific to your code:Separation of concerns.  Each function should do one thing; the function's name should describe its purpose.  It seems obvious to have a main() that reads the input and prints the result.  The check should be done in an isNDrome() function.Code reuse.  If everything is in main(), the only way to call your code is by running your program on the command line and writing to its standard input.  Forget about ever putting a graphical or web interface on it.  Your only reasonable code reuse mechanism is cut-and-paste.Unit testing.  As it stands, it's barely possible.Simpler flow of control.  You used a named break statement (with a label poorly named here, I might add).  You also used a cryptically named variable (y) to store the result.  When you discover that the string is not an N-drome, you have to set the flag (y = 0) then execute a jump (break).  All of that would be cleaner with if your code were in an isNDrome() function: just return false as soon as you discover a mismatch.As I mentioned, your code is hard to read.  A significant contributor to the problem is the proliferation of cryptically named variables: y, i, j, x, a, b.  Only n is self-explanatory due to the nature of the problem.  You need to reduce the number of variables and name them better.Performance-wise, string concatenation is considered to be a bad idea in Java.  Since strings are immutable, each concatenation creates a new string.  The innocuous-looking a = a + original.charAt(i) actually results in the following bytecode (which you can see by running javap -c NDrome): 103: new           #16                 // class java/lang/StringBuilder 106: dup            107: invokespecial #17                 // Method java/lang/StringBuilder.<init>:()V 110: aload         8 112: invokevirtual #18                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 115: aload_2        116: iload         5 118: invokevirtual #19                 // Method java/lang/String.charAt:(I)C 121: invokevirtual #20                 // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder; 124: invokevirtual #21                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String; 127: astore        8In other words, it's equivalent toa = (new StringBuilder(a)).append(original.charAt(i)).toString();Your code probably isn't performance-critical, but it would still be a good habit to avoid concatenating strings carelessly.Proposed solutionThe loop can be simplified to just half a dozen lines.public static boolean isNDrome(CharSequence cs, int n) {    int len = cs.length();    if (n > len) {        // Controversial, in my opinion.  Arguably, the function should        // just return false (which it would do naturally if you just        // remove this check altogether.).        throw new IllegalArgumentException(N exceeds input length);    }    if (len % n != 0) {        return false;    }    int chunks = len / n;    for (int a = 0, z = chunks - 1; a < z; a++, z--) {        // Compare chunk a, containing n characters from the first half        // of the string, with chunk z, its counterpart from the second        // half.        CharSequence aChars = cs.subSequence(a * n, (a + 1) * n),                     zChars = cs.subSequence(z * n, (z + 1) * n);        if (!aChars.equals(zChars)) {            return false;        }    }    return true;}The code to call it can also look cleaner as a result.public static void main(String[] args) {    try (Scanner in = new Scanner(System.in)) {        System.out.print(enter a string: );        String original = in.nextLine();        System.out.print(enter the n value: );        int n = in.nextInt();        try {            System.out.printf(%s %s a %d-Drome\n,                              original,                              isNDrome(original, n) ? is : is not,                              n);        } catch (IllegalArgumentException nExceedsLength) {            System.out.printf(Error: %s! Exiting.\n,                              nExceedsLength.getMessage());        }    }}