Valid Palindrome

class Solution {
		public boolean isPalindrome(String s) {
				if (s == null || s.length() <= 1) return false;
				//clean the space and capital letters 
				StringBuilder str = new StringBuilder();
				for (int i = 0; i < s.length(); i++) {
					if (s.charAt(i) != ' ') {
						str.append(s.toLowerCase().charAt(i));
					} else {
						i++;
					}
				}
				//two pointers to loop the new string
				String strStr = str.toString();
				int k = 0;
				int j = strStr.length() - 1;
				while (k < j) {
					if (strStr.charAt(k) != strStr.charAt(j)) return false;
					if (strStr.charAt(k) == strStr.charAt(j)) {
						k++;
						j--;
					}
				}
				return true;
		}
}

最后更新于

这有帮助吗?