আর্টিকেল: JavaScript Palindrome Checker: How to Determine if an Integer is a Palindrome

আর্টিকেল: JavaScript Palindrome Checker: How to Determine if an Integer is a Palindrome

Master JavaScript problem-solving on LeetCode with practical tips, solutions, and examples in this comprehensive Bangla guide. Enhance your JavaScript

আচ্ছা ধরে নেই , আমরা একটা ওয়েব এপ্লিকেশন এ কাজ করছি যেখানে users বিভিন্ন কাজের জন্য ইনপুট দিতে পারে , like account balances, transaction amounts, or unique identifiers । আমাদের গোল হচ্ছে ensure করা user যে ডাটা দিয়েছে তা accurate এবং trustworthy । এই ধরণের প্রব্লেম গুলোতে আমাদের চেক করার দরকার হয় user এর দেয়া নাম্বার palindrome কিনা। palindrome কে একভাবে ভ্যালিডেশন বলা যেতে পারে।

palindrome মূলত একটা মেথমেটিক্যাল কনসেপ্ট। যা রেফার করে ইনপুট এ দেয়া নাম্বার একই কিনা যদি, সেটাকে রিভার্স করা হয়। For example, numbers like 121, 1331 , এবং 12321 হচ্ছে palindromes, তবে numbers like 123, 456, এবং 12345 এগুলি আবার palindrome নাম্বার না।

আমরা আজকের আর্টিকেল এ জাভাস্ক্রিপ্ট দিয়ে leetcoder এর Palindrome Number প্রব্লেম টি সল্ভ করার চেষ্টা করব।

আমরা যদি https://leetcode.com/problems/palindrome-number url এ যাই, তবে leetcode এর Palindrome Number প্রব্লেম টি দেখতে পাব।

#Problem_Explanation:

নিচের ইমেজ এ খেয়াল করলে আমরা স্পেসিফিক প্রব্লেম দেখতে পাব যা আমাদের সল্ভ করতে হবে :

Problem Image :

Problem Image :

এখানে বলা হচ্ছে , আমাদের একটা ফাঙ্কশন লিখতে হবে যেটা পেরামিটার আকারে ইনপুট নিবে ইন্টিজার x এবং x যদি palindrome হয় তবে, রিটার্ন করতে হবে true আর যদি, palindrome না হয় তবে রিটার্ন করবে false

Example:

leetcode আমাদের ৩ তা এক্সাম্পল দিয়েছে । যার উপর ভিত্তি করে আমরা বুজতে পারি সল্যুশন গুলি কেমন হবে এবং আমরা code কিভাবে লিখব :

Example

Example 1:

প্রথম example এ বুঝান হয়েছে , input integer x যদি 121 হয় এবং এটি একটি palindrome, আমরা যদি সংখ্যা টিকে উল্টে পাল্টে মানে, রিভার্স করি তাতে করে রেজাল্ট একই ১২১ ই হবে :

নিচের প্রসেস ফলো করে আমরা কোড করতে পারি :

  • The function is called x equal to 121.

  • It checks that x is not negative, which is true in this case.

  • Then, it enters a loop to reverse the digits of x.

  • In this case, the digits are 1, 2, and 1.

  • The loop reverses them, resulting in 121.

  • Finally, it compares the original x with the reversed version, which is also 121. Since they match, the function returns true, indicating that 121 is a palindrome.

Example 2:

দ্বিতীয় example এ , input integer x তবে,যদি নেগেটিভ হয় (-121), আমাদের palindrome চেক করতে হবে ,

we follow a similar process:

  • The function checks that x is not negative, which is not true because x is negative (-121).

  • Since x is negative, it immediately returns false, indicating that negative numbers are not considered palindromes in this context.

Example 3:

তৃতীয় example এর input integer x হচ্ছে 10 ।

আর তার জন্য প্রসেস হবে :

  • The function checks that x is not negative, which is true in this case.

  • It enters the loop to reverse the digits x, but the reversed version becomes 01.

  • Finally, it compares the original x (10) with the reversed version (01), which is not the same. Therefore, the function returns false, indicating that 10 is not a palindrome.

Constraints:

Constraints হচ্ছে একটা গাইডেন্স যা, আমাদের বলে কি ধরণের input ভ্যালু এই প্রব্লেম এর জন্য ভ্যালিড। কিছু স্পেশাল রুলস বলে দেয়া হয় বেটার performance এর জন্য।

কন্সট্রেটস বোঝাটা জরুরি ,আমরা যখন code ডিসাইন ও ইম্প্লিমেন্টেশন সল্যুশন লিখব এটা আমাদের একটা বাউন্ডারি বলে দেয় ,কি করা উচিত।

These constraints specify the valid range of input values for the integer x in the problem. Let's break down what this means:

  • -231: এখানে বুঝান হয়েছে x এর minimum valid value -2,147,483,648 থেকে কম হতে পারবে না।

  • 231 - 1: আর x maximum valid value 2,147,483,647 থেকে বেশি হতে পারবে না।

আমাদের সল্যুশন ডিসাইন করার সময় এই ইনপুট মাথায় রাখতে হবে। তবে এই টেস্ট আমাদেরকে leetcode করে দিবে যখন আমরা কোড সাবমিট করব। মূলত ম্যাথমেটিক্স ক্যাল্কুলেশন এর মাধ্যমে এই সংখ্যার রেঞ্জ বেরকরা হয়।

#How_We'll_Do_It:

সল্যুশন কোড দেখার আগে বেটার অপসন হচ্ছে নিজে নিজে প্রব্লেম বুঝে প্রাকটিস করা।
প্রব্লেম সলভিং এর একমাত্র টার্গেট হচ্ছে আমাদের থিঙ্কিং প্রসেস ও লজিক বিল্ড করা। তাই আমি ,ইনক্রেস করছি নিজে প্রথমে প্রব্লেম বুঝে চেষ্টা করা উচিত।

Watch Video Solution:

Writing the JavaScript Code:

//solution code 
const isPalindrome = function (x) {
  const xStr = x.toString();

  let left = 0;
  let right = xStr.length - 1;
  for (left; left < right; left++, right--) {
    if (xStr[left] !== xStr[right]) {
      return false;
    }
  }
  return true;
};

আমরা এখানে ফরলুপ ব্যবহার করে এফেক্টেবলয় input integer x এর শুরুর এবং শেষের স্ট্রিং ক্যারেকটার কম্পেয়ার করে চেক করেছি আমাদের দেয়া x ইন্টিজার palindrome কিনা। যদি সব ক্যারেক্টর ম্যাচ করে তবে true রিটার্ন করবে অন্যথায় false রিটার্ন করবে।

#Explain_in_Action:

Let's break down and explain the provided code step by step:

// Solution code
const isPalindrome = function (x) {

আমরা একটা ফাঙ্কশন লিখেছি isPalindrome নামে that takes an integer x as its input parameter.

  const xStr = x.toString();

আমরা x পেরামিটার কে স্ট্রিং এ কনভার্ট করেছি এবং xStr নামে ভ্যারিয়েবল এ স্টোর করেছি। এই কনভার্ট করা জরুরি কারণ আমরা যখন palindromes চেক করব প্রত্যেকটি ক্যারেক্টর কম্পেয়ার করতে হবে। স্ট্রিং হলে সহজে আমরা কম্পেয়ার করতে পারব ক্যারেক্টর এর সাথে ক্যারেক্টর এর।

  let left = 0;
  let right = xStr.length - 1;

আমরা দুইটি পয়েন্টার নিয়েছি , left এবং right নামে। left pointer স্ট্রিং এর (index 0) থেকে শুরু করবে ,এবং right pointer স্ট্রিং এর (index xStr.length - 1) মানে শেষ থেকে ১ কমিয়ে দিবে। ইনডেক্স pointer গুলোর উপর ভিত্তি করে আমরা ক্যারেক্টর এর মাধ্যমে palindrome চেক করব ।

  for (left; left < right; left++, right--) {

আমরা একটা ফর লুপ চালাবো যেখানে কন্ডিশন হচ্ছে left যদি right এর থেকে ছোট হয় তবে প্রত্যেক ইটারেশন এর সময় left ++right-- করবে।

    if (xStr[left] !== xStr[right]) {

লুপ এর মধ্যে if স্টেটমেন্ট এ ক্যারেক্টর এর left and right পসিশন compare করবে স্ট্রিং এর। যদি দুইটা সমান না হয় তবে ইন্টিজার টি palindrome না।

      return false;

return করবে false কারণ, স্ট্রিং ক্যারেক্টর পসিশন সমান হয় নি এবং false মানে এটা palindrome না।

    }
  }

  return true;

কোন কন্ডিশন ম্যাচ করে না এমন ক্যারেক্টর এর সমুখীন না হয়েই লুপটি শেষ হয়, তাহলে এর অর্থ হল স্ট্রিং-এর সমস্ত ক্যারেক্টর মিলে যাবে। যখন left এবং right থেকে center এর সাথে তুলনা করে। এই ক্ষেত্রে, আমরা লুপ থেকে বের হয়ে যাই এবং true রিটার্ন করি।

true রিটার্ন করা মানে ,পূর্ণ সংখ্যাটি একটি প্যালিনড্রোম । মূলত true রিটার্ন প্রমান করে এই কন্ডিশন টি প্যালিনড্রোম।

#Handling_Special_Cases:

আমরা while লুপ ব্যবহার করেও problem এর সল্যুশন করতে পারি :

const isPalindrome = function (x) {
  const xStr = x.toString();

  let left = 0;
  let right = xStr.length - 1;

  while (left < right) {
    if (xStr[left] !== xStr[right]) {
      return false;
    }

    left++;
    right--;
  }

  return true;
};

#What's_Next?:

🤝
Try creating your counting function in JavaScript, and experiment with it in different scenarios. If you have any questions or thoughts, feel free to share them in the comments below.

Happy coding! 🚀

#problemsolving #problemsolver #javascript #tanvir_mehedi #leetcode