Write a program to check for valid palindrome string.
Example 1:
Input: str="A man, a plan, a canal: Panama"
Output: true
Approach: With memory
Java
public class ValidPalindromeString {public static void main(String[] args) {String str = "A man, a plan, a canal: Panama";if (isPalindrome(str)) {System.out.println("true");} else {System.out.println("false");}}// method to check string is Palindromepublic static boolean isPalindrome(String s) {// define two stringString str1 = "";String str2 = "";// iterate till end of stringfor (int i = 0; i < s.length(); i++) {// add when only letterOrDigitif (Character.isLetterOrDigit(s.charAt(i))) {// append in laststr1 = str1 + s.charAt(i);// append in reverse orderstr2 = s.charAt(i) + str2;}}// check both are equalsif (str1.equalsIgnoreCase(str2))return true;return false;}}
C++
#include <bits/stdc++.h>using namespace std;//function to check string is Palindromebool isPalindrome(string s){//store the each charater into//the vector only alphanumeric valuesvector<char> v;for(int i=0;i<s.size();i++){if(isalpha(s[i])){//convert into the lower caseif(s[i]>='A'&&s[i]<='Z')s[i]=s[i]+32;v.push_back(s[i]);}if(isdigit(s[i]))v.push_back(s[i]);}//check for palindromeint i=0,j=v.size()-1;while(i<j){//if correspondings are not//equal return falseif(v[i]!=v[j])return false;i++;j--;}//return truereturn true;}int main(){string str = "A man, a plan, a canal: Panama";if (isPalindrome(str)) {cout<<"true";;} else {cout<<"false";}}
Approach: Effective
Java
public class ValidPalindromeString1 {public static void main(String[] args) {String str = "A man, a plan, a canal: Panama";if (isPalindrome(str)) {System.out.println("true");} else {System.out.println("false");}}// method to check string is Palindromepublic static boolean isPalindrome(String s) {// hold length start and endint left = 0;int right = s.length() - 1;// iterate untill left less then rightwhile (left < right) {// iterate untill not letter or digit from beginningwhile (left < right && !Character.isLetterOrDigit(s.charAt(left))) {left++;}// convert lower caseString l = String.valueOf(s.charAt(left)).toLowerCase();// iterate untill not letter or digit from endwhile (left < right && !Character.isLetterOrDigit(s.charAt(right))) {right--;}// convert lower caseString r = String.valueOf(s.charAt(right)).toLowerCase();// if !equals then falseif (!l.equals(r))return false;left++;right--;}return true;}}
C++
#include <bits/stdc++.h>using namespace std;//function to check string is Palindromebool isPalindrome(string s){// hold length start and endint left = 0;int right = s.length() - 1;// iterate untill left less then rightwhile (left < right){// iterate untill not letter or digit from beginningwhile (left < right && !isalpha(s[left])&&!isdigit(s[left])) {left++;}// convert lower caseif(s[left]>='A'&&s[left]<='Z')s[left]=s[left]+32;// iterate untill not letter or digit from endwhile (left < right && !isalpha(s[right])&&!isdigit(s[right])) {right--;}// convert lower caseif(s[right]>='A'&&s[right]<='Z')s[right]=s[right]+32;// if !equals then falseif (s[left]!=s[right])return false;left++;right--;}return true;}int main(){string str = "A man, a plan, a canal: Panama";if (isPalindrome(str)) {cout<<"true";;} else {cout<<"false";}}
No comments:
Post a Comment