Skip to main content

Posts

Showing posts from March, 2026

Two Pointers:- Valid Palindrome

  Valid Palindrome A phrase is a  palindrome  if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. Given a string  s , return  true  if it is a  palindrome , or  false  otherwise . Example 1: Input: s = "A man, a plan, a canal: Panama" Output: true Explanation: "amanaplanacanalpanama" is a palindrome. Example 2: Input: s = "race a car" Output: false Explanation: "raceacar" is not a palindrome. Example 3: Input: s = " " Output: true Explanation: s is an empty string "" after removing non-alphanumeric characters. Since an empty string reads the same forward and backward, it is a palindrome. Solution: Reverse String in python: [::-1] Check if character is alpha numeric(to avoid " , "): .isalnum() class Solution : def isPalindrome ( self , s : str ) -> b...

Arrays & Hashing:- Valid Anagram | Two Sum | Group Anagrams

Valid Anagram Given two strings s and t, return true if t is an anagram of s, and false otherwise. Example 1: Input: s = "anagram", t = "nagaram" Output: true Example 2: Input: s = "rat", t = "car" Output: false Solution : class Solution ( object ):     def isAnagram ( self , s , t ):         """         :type s: str         :type t: str         :rtype: bool         """         if len (s)!= len (t):             return False                 countS, countT= {},{}         for i in range ( len (s)):             countS[s[i]]= 1 + countS.get(s[i], 0 )             countT[t[i]]= 1 + countT.get(t[i], 0 )                 return countS == countT         T...