fork download
  1. // your code goes here
  2. function isPalindrome(str) {
  3. // let rev_str = "";
  4. // for(let i=str.length-1;i>=0;i--) {
  5. // rev_str += str[i];
  6. // }
  7. // if(rev_str == str) {
  8. // return true;
  9. // }
  10. // return false;
  11. let left=0, right=str.length-1;
  12. while(left<right) {
  13. if(str[left]!=str[right]) {
  14. return false;
  15. }
  16. left++;
  17. right--;
  18. }
  19. return true;
  20. }
  21.  
  22. // TC: O(n)
  23. // SC: O(n)
  24.  
  25. console.log(isPalindrome("malayalam"))
Success #stdin #stdout 0.02s 19080KB
stdin
Standard input is empty
stdout
true