简体   繁体   中英

understanding compareto() method in java

i came across a MIT lecture on recursion where they checked palindrome using recursion,and there they checked it using logic where they compare first and last alphabet and so on.. what i thought is something as follows

just a pseudo code:

    String original="abba";

    String reverse = "";
    for(int i=original.length()-1;i>=0;i--)
    {
        reverse+=new String(original.substring(i,i+1));
    }
    if(original.equals(reverse))
        System.out.println("palindrome");
    else
        System.out.println("not palindrome");

i have two doubts

  • Is this logic better than recursion or conventional logic in terms of time complexity?
  • How does compareTo() method checks if strings are equal? DOes it compare bytecode or something?

You should use equals , not compareTo , since compareTo returns an int and not a boolean as your if condition would expect. Alternately you can check if (original.compareTo(reverse)==0) , which means the Strings are equal.

As to how compareTo works, it compares each pair of characters having the same index, and returns the difference between the first non equal pair. If all are equal, 0 is returned.

CompareTo will loop over each character in the strings. If the character is bigger/smaller (based on alphabetical order) it will return a positive/negative number.

Therefore when string1.compareTo(reverseString)==0 the two strings are the same. (no single letter is smaller/bigger than the one in the other string)

Since you didn't provide a recursive implementation its hard to say anything about performance characteristics.

Is this logic better than recursion or conventional logic in terms of time complexity?

Assuming you are using the following recursive method,

public static boolean isPal(String s)
    {
        if(s.length() == 0 || s.length() == 1)
            return true; 
        if(s.charAt(0) == s.charAt(s.length()-1))
            return isPal(s.substring(1, s.length()-1));
        return false;
    }

Time Complexity: O(n)

equals:

public boolean equals(Object paramObject)
  {
    if (this == paramObject) {
      return true;
    }
    if ((paramObject instanceof String))
    {
      String str = (String)paramObject;
      int i = this.value.length;
      if (i == str.value.length)
      {
        char[] arrayOfChar1 = this.value;
        char[] arrayOfChar2 = str.value;
        for (int j = 0; i-- != 0; j++) {
          if (arrayOfChar1[j] != arrayOfChar2[j]) {
            return false;
          }
        }
        return true;
      }
    }
    return false;
  }

The complexity of your program is : O(n)+O(n)=O(n)


How does compareTo() method checks if strings are equal? DOes it compare bytecode or something?

compareTo: returns 0 if the two strings are same or else it returns the difference, integer difference (ie difference of Unicode values) of first non-matching characters,

It does so by comparing characters at same positions of two strings

  public int compareTo(String paramString)
  {
    int i = this.value.length;
    int j = paramString.value.length;
    int k = Math.min(i, j);
    char[] arrayOfChar1 = this.value;
    char[] arrayOfChar2 = paramString.value;
    for (int m = 0; m < k; m++)
    {
      int n = arrayOfChar1[m];
      int i1 = arrayOfChar2[m];
      if (n != i1) {
        return n - i1;
      }
    }
    return i - j;
  }

1: is this logic better than recursion or coventional logic in terms of time complexity In Java recursion is fairly expensive compared to iteration (in general) because it requires the allocation of a new stack frame. If you're using a functional language like C, recursion might be faster. If you're using an imperative language like Java, iteration is probably faster.

2:how compareTo() method checks if strings are equal

The value 0 if the argument is a string lexicographically equal to this string; a value less than 0 if the argument is a string lexicographically greater than this string; and a value greater than 0 if the argument is a string lexicographically less than this string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM