I have this method, isPalindrome(), and I am trying to find the time complexity of it, and also rewrite the code more efficiently.
boolean isPalindrome(String s) {
boolean bP = true;
for(int i=0; i<s.length(); i++) {
if(s.charAt(i) != s.charAt(s.length()-i-1)) {
bP = false;
}
}
return bP;
}
Now I know this code checks the string’s characters to see whether it is the same as the one before it and if it is then it doesn’t change bP.
And I think I know that the operations are s.length(), s.charAt(i) and s.charAt(s.length()-i-!)).
Making the time-complexity O(N + 3), I think? This correct, if not what is it and how is that figured out.
Also to make this more efficient, would it be good to store the character in temporary strings?
The given code appears to be checking if a string is a palindrome by checking if character “N” is the same as character “length-N”. As already mentioned, you can increase the efficiency by
To those suggestions, I’d add
Given all that: