โ— LIVE
OpenAI releases GPT-5 APIIndia AI startup raises $120MBitcoin ETF hits record inflowsMeta Llama 4 benchmarks leakedOpenAI releases GPT-5 APIIndia AI startup raises $120MBitcoin ETF hits record inflowsMeta Llama 4 benchmarks leaked
๐Ÿ“… Mon, 23 Mar, 2026โœˆ๏ธ Telegram
AiFeed24

AI & Tech News

๐Ÿ”
โœˆ๏ธ Follow
๐Ÿ Home๐Ÿค–AI๐Ÿ’ปTech๐Ÿš€Startupsโ‚ฟCrypto๐Ÿ”’Security๐Ÿ‡ฎ๐Ÿ‡ณIndiaโ˜๏ธCloud๐Ÿ”ฅDeals
โœˆ๏ธ News Channel๐Ÿ›’ Deals Channel
Home/Cloud & DevOps/Valid Anagram in Python Using Two Methods (Sorting & Counting)
โ˜๏ธCloud & DevOps

Valid Anagram in Python Using Two Methods (Sorting & Counting)

Problem Explanation You are given two strings s and t. True if t is an anagram of s, otherwise return False. An anagram means both strings contain the same characters with the same frequency, just in a different order. Input: s = "anagram", t = "nagaram" True Input: s = "rat", t = "car" False Method

โšกQuick SummaryAI generating...
S

Sri Mahalakshmi

๐Ÿ“… Mar 22, 2026ยทโฑ 3 min readยทDev.to โ†—
โœˆ๏ธ Telegram๐• TweetWhatsApp
๐Ÿ“ก

Original Source

Dev.to

https://dev.to/sri_mahalakshmi_a87f9d361/valid-anagram-in-python-using-two-methods-sorting-counting-mm2
Read Full โ†—

Problem Explanation

You are given two strings s and t.
Your task is to return True if t is an anagram of s, otherwise return False.

An anagram means both strings contain the same characters with the same frequency, just in a different order.

Example:

  • Input: s = "anagram", t = "nagaram"
    Output: True

  • Input: s = "rat", t = "car"
    Output: False

Method 1: Sorting (Simplest Approach)

Idea

If two strings are anagrams, their sorted forms will be equal.

Code

class Solution:
    def isAnagram(self, s, t):
        return sorted(s) == sorted(t)

Explanation

  • sorted(s) โ†’ sorts characters of string s
  • sorted(t) โ†’ sorts characters of string t
  • If both sorted results are equal โ†’ strings are anagrams

Why Use This?

  • Very easy to write and understand
  • Best for beginners
  • Clean one-line solution

Method 2: Counting Characters (Optimal Approach)

Idea

Count how many times each character appears in both strings and compare.

Code

class Solution:
    def isAnagram(self, s, t):
        if len(s) != len(t):
            return False

        count = [0] * 26

        for i in range(len(s)):
            count[ord(s[i]) - ord('a')] += 1
            count[ord(t[i]) - ord('a')] -= 1

        for num in count:
            if num != 0:
                return False

        return True

Explanation (Step-by-Step)

  • if len(s) != len(t):
    If lengths differ โ†’ cannot be anagrams

  • count = [0] * 26
    Create a list to store frequency of each character

  • for i in range(len(s)):
    Loop through both strings

  • count[...] += 1
    Increase count for character in s

  • count[...] -= 1
    Decrease count for character in t

  • if num != 0:
    If any count is not zero โ†’ not anagram

  • return True
    All counts match โ†’ valid anagram

Key Takeaway

  • Sorting โ†’ simplest and clean
  • Counting โ†’ more efficient and optimal
Tags:#cloud#dev.to

Found this useful? Share it!

โœˆ๏ธ Telegram๐• TweetWhatsApp

Read the Full Story

Continue reading on Dev.to

Visit Dev.to โ†—

Related Stories

โ˜๏ธ
โ˜๏ธCloud & DevOps

Hiring Senior Full Stack Developer (Remote, USA)

about 2 hours ago

โ˜๏ธ
โ˜๏ธCloud & DevOps

How I Built a Multi-Tenant WhatsApp Automation Platform Using n8n and WAHA

about 2 hours ago

โ˜๏ธ
โ˜๏ธCloud & DevOps

I Built an Instant SEO Audit API โ€” Here's What I Learned About Technical SEO

about 3 hours ago

โ˜๏ธ
โ˜๏ธCloud & DevOps

SJF4J: A Structured JSON Facade for Java

about 3 hours ago

๐Ÿ“ก Source Details

Dev.to

๐Ÿ“… Mar 22, 2026

๐Ÿ• 1 day ago

โฑ 3 min read

๐Ÿ—‚ Cloud & DevOps

Read Original โ†—

Web Hosting

๐ŸŒ Hostinger โ€” 80% Off Hosting

Start your website for โ‚น69/mo. Free domain + SSL included.

Claim Deal โ†’

๐Ÿ“ฌ AiFeed24 Daily

Top 5 AI & tech stories every morning. Join 40,000+ readers.

โœฆ 40,218 subscribers ยท No spam, ever

Cloud Hosting

โ˜๏ธ Vultr โ€” $100 Free Credit

Deploy cloud servers in 25+ locations. From $2.50/mo. No contract.

Claim $100 Credit โ†’
AiFeed24

India's AI-powered tech news hub. Daily coverage of AI, startups, crypto and emerging technology.

โœˆ๏ธ๐Ÿ›’

Topics

Artificial IntelligenceStartups & VCCryptocurrencyCybersecurityCloud & DevOpsIndia Tech

Company

About AiFeed24Write For UsContact

Daily Digest

Top 5 AI stories every morning. 40,000+ readers.

No spam, ever.

ยฉ 2026 AiFeed24 Media.Affiliate Disclosure โ€” We earn commission on qualifying purchases at no extra cost to you.
PrivacyTermsCookies