Advertisement
uopspop

Untitled

Aug 9th, 2021
985
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.44 KB | None | 0 0
  1. class Solution {
  2.     public boolean isPalindrome(String s) {
  3.         // convert to lower cases
  4.         s = s.toLowerCase();
  5.        
  6.         Integer i_left = -1;
  7.         Integer i_right = s.length();
  8.        
  9.         while (true) {
  10.            
  11.             Character c_left = null;
  12.             Character c_right = null;
  13.            
  14.             // move i_left, i_right to the meaning position
  15.             while (true) {
  16.                 i_left++;
  17.                 if (i_left == s.length()) break;
  18.                 if (is_character(s.charAt(i_left))) {
  19.                     c_left = s.charAt(i_left);
  20.                     break;
  21.                 }
  22.             }
  23.            
  24.             while (true) {
  25.                 i_right--;
  26.                 if (i_right < 0) break;                
  27.                 if (is_character(s.charAt(i_right))) {
  28.                     c_right = s.charAt(i_right);
  29.                     break;
  30.                 }
  31.             }
  32.            
  33.             if (i_left > i_right) break;
  34.            
  35.             if (c_left != c_right) {
  36.                 // wrong
  37.                 return false;
  38.             }
  39.            
  40.         }
  41.        
  42.         return true;
  43.        
  44.     }
  45.    
  46.     public boolean is_character(char c) {
  47.         if (c >= 'a' && c <= 'z') {
  48.             return true;
  49.         }else if (c >= '0' && c <= '9') {
  50.             return true;
  51.         }
  52.        
  53.         return false;
  54.     }
  55.    
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement