12: Check if a string is a palindrome.
⏳⌚ 00:00:00

Question:-

Check if a string is a palindrome.
Example 1:
Input:
str = "madam"
Output: "yes"
Example 2:
Input:
str = "world"
Output: "no"

Steps to solve:-

1. Input:- take a string input from user.
2. size of string:- we find string size by int n = str.length().
3. declare a bool variavbe:-
We start with a boolean variable isPalindrome initialized to true.
4. outer loop Inside the for loop, we compare characters from both ends of the string (str[i] and str[j]) and set isPalindrome to false if they don't match. We also break out of the loop early since we've already determined that the string is not a palindrome.
5. final:- After the loop, we use an if statement to check the value of isPalindrome. If it's true, we print that the string is a palindrome; otherwise, we print that it's not a palindrome.

solution->

View Code 1
Time Complexity = O(n)
Space Complexity = O(1)