From 68948a280294b701e74e71dc6365308a829452ef Mon Sep 17 00:00:00 2001 From: ifenil Date: Sat, 3 Oct 2020 19:59:44 +0530 Subject: [PATCH 1/2] Valid_Palindrome --- .../Valid_Palindrome.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Leetcode/Leetcode_August_Challenge/Valid_Palindrome.java diff --git a/Leetcode/Leetcode_August_Challenge/Valid_Palindrome.java b/Leetcode/Leetcode_August_Challenge/Valid_Palindrome.java new file mode 100644 index 0000000..0981d09 --- /dev/null +++ b/Leetcode/Leetcode_August_Challenge/Valid_Palindrome.java @@ -0,0 +1,38 @@ +package Leetcode_August_Challenge; + +public class Valid_Palindrome { + + public boolean isPalindrome(String s) { + + if(s==null){ + return false; + } + + s = s.toLowerCase(); + + int i=0; + int j=s.length()-1; + + while(i='a' && s.charAt(i)<='z') + || (s.charAt(i)>='0'&&s.charAt(i)<='9'))){ + i++; + } + + while(i='a' && s.charAt(j)<='z') + || (s.charAt(j)>='0'&&s.charAt(j)<='9'))){ + j--; + } + + if(s.charAt(i) != s.charAt(j)){ + return false; + } + + i++; + j--; + } + + return true; + + } +} From e25659fe897057ad0f72f0433d21b00525789af7 Mon Sep 17 00:00:00 2001 From: ifenil Date: Sat, 3 Oct 2020 20:08:25 +0530 Subject: [PATCH 2/2] Valid_Palindrome --- .../Valid_Palindrome.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/Leetcode/Leetcode_August_Challenge/Valid_Palindrome.java b/Leetcode/Leetcode_August_Challenge/Valid_Palindrome.java index 0981d09..bdc0bb8 100644 --- a/Leetcode/Leetcode_August_Challenge/Valid_Palindrome.java +++ b/Leetcode/Leetcode_August_Challenge/Valid_Palindrome.java @@ -3,17 +3,25 @@ public class Valid_Palindrome { public boolean isPalindrome(String s) { - + // check string if it's null no need to go further direct return false + if(s==null){ return false; } - + + // if string contain some char as uppercase or lowercase + // this will convert whole string to lowercase s = s.toLowerCase(); + // pointed i to intially at 0 + // and j to end of the string (last character) int i=0; int j=s.length()-1; + while(i true + while(i='a' && s.charAt(i)<='z') || (s.charAt(i)>='0'&&s.charAt(i)<='9'))){ i++; @@ -23,15 +31,16 @@ public boolean isPalindrome(String s) { || (s.charAt(j)>='0'&&s.charAt(j)<='9'))){ j--; } - + // if one of string char not match i to j + // it will return false if(s.charAt(i) != s.charAt(j)){ return false; } - + i++; j--; } - + // else return true return true; }