Reputation: 45
What is the time complexity of the below code which checks whether the given input is palindrome or not?
public boolean isPalindromeRecursion(String input, int first, int last) {
if (input.charAt(first) != input.charAt(last)) {
return false;
} else if (first >= last) {
return true;
}
return isPalindromeRecursion(input, first + 1, last - 1);
}
Upvotes: 1
Views: 357
Reputation: 11136
Time complexity of your algorithm, is:
O(n/2)
;O(n)
, as the constant factors are disregarded when we use Big O analysis, and it's good that they are disregarded.Upvotes: 4