[LeetCode]242. Valid Anagram(有效字谜)

Python习题集:基础篇 【说明】本文练习题均整理自imooc教程初识Python(课程已下架),答案为原创,用于个人学习与巩固,如有更好方案,欢迎大神赐教! ======================================================== 1.计算十进制整数 45678 和十六进制整数 0x12fd2 之和。 print 45678+0x12fd2 2.请用两种方式打印出 hello, python. #input code print 'hello, python.' print 'hello, 阅读详情

242. Valid Anagram

Given two strings s and t, write a function to determine if t is an anagram of s.
给定两个字符串s和t,写一个函数来确定t是否是s的字谜。

For example

s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.

Note:
You may assume the string contains only lowercase alphabets.
(假定只有小写字母)

其实就是判断第一个字符串和第二个字符串是否包含同样的字母,顺序不一样而已

思路:

  • 第一反应用异或,用了好多例子回忆异或
  • 将字符串排序,比较各个位置上字母是否相同(排序导致效率低下)

代码如下:

#include <iostream>
#include <string>
#include<algorithm>
using namespace std;
class Solution {
public:
    bool isAnagram(string s, string t) {
        if(s.size() != t.size())//判断两个字符串长度是否相同
            return false;
        if(s.size() == 0)//判断两个字符串长度是否为空
            return true;
        sort(s.begin(), s.end());//将两个字符串排序
        sort(t.begin(), t.end());
        bool res;
        for(int i=0; i<s.size(); i++){
            res = s[i]^t[i];//相同为0 不同为1
            if(res == 1)
                return false;
        }
        return true;
    }
};
int main()
{
    //0 false; 1 true
    //异或 相同为0 不同为1
    /*
    char a0 = 'a';
    char a1 = 'b';
    char a2 = 'a';
    bool b = a0^a1;//1 bool类型
    int b1 = a0^a1;//3 int类型
    bool c = a0^a2;//0
    bool d = (a0^a1)^(a1^a0);//0
    b ^= (a1^a0);//1
    b1 ^= (a1^a0);//0
    cout << b << c << d << b1<< endl;//1 0 0 0
    */

    Solution a;
    string s = "aa";
    string t = "bb";
    string s1 = "ab";
    string t1 = "ba";
    cout << a.isAnagram(s, t) << endl;
    cout << a.isAnagram(s1, t1) << endl;
    return 0;
}
  • 写完之后才发现,根本不用比较一个个的字符,排序后两字符串应该完全相同,直接比较字符串就行。贴出代码:
class Solution {
public:
    bool isAnagram1(string s, string t) {
        sort(s.begin(), s.end());
        sort(t.begin(), t.end());
        return (s == t);
    }
};
  • 随后又想出来一个方法,定义一个数组counts来存储各个字母数量(默认值为0),字符串s增加各个字母数量,字符串t减少各个字母数量,最后判断数组counts是否每个元素都是0,只要有一个元素不为0,返回false,否则返回true,贴出代码:
bool isAnagram2(string s, string t) {
        if(s.size() != t.size())//判断两个字符串长度是否相同
            return false;
        int counts[26] = {0};
        for(int i=0; i<s.size(); i++){
            counts[s[i]-'a']++;
            counts[t[i]-'a']--;
        }
        for(int m=0; m<26; m++){
            if(counts[m])//counts[m]!=0
                return false;
        }
        return true;
    }
  • 在网上看到了一个利用Hash Table的方法,思路大致相同
class Solution {
public:
    bool isAnagram(string s, string t) {
        if (s.length() != t.length()) return false;
        int n = s.length();
        unordered_map<char, int> counts;
        for (int i = 0; i < n; i++) {
            counts[s[i]]++;
            counts[t[i]]--;
        }
        for (auto count : counts)
            if (count.second) return false;
        return true;
    }
};
242. Valid Anagram [easy] (Python) 题目链接https://leetcode.com/problems/valid-anagram/题目原文 Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = “anagram”, t = “nagaram”, return true. 阅读详情

相关推荐

Hypermesh二次开发实战:从Tcl/Tk脚本到Ext API的进阶之路

本文系统介绍了Hypermesh二次开发的进阶路径,从自动化重复操作的Tcl/Tk脚本入门,到利用HMASCII接口深度读写模型数据,最终深入Ext API进行高性能计算与功能定制。通过实战案例,帮助工程师掌握从脚本录制到C++插件开发的完整技能,显著提升CAE前处理效率与自动化水平。

weixin_29306875的博客 683

242. Valid Anagram python

给定字符串s和t,编写函数判断t是否为s的anagram(字谜游戏,由颠倒字母顺序而构成的字[短语])。class Solution(object): def isAnagram(self, s, t): """ :type s: str :type t: str :rtype: bool """ ...

mario_mmh的博客 293

使用基于物质点法(MPM)的Anura3D模拟沙柱坍塌

本博客使用开源软件Anura3D软件模拟了一个沙柱坍塌的过程,这个软件是基于基于物质点法(MPM)设计的

weixin_59902634的博客 4062

Leetcode做题日记:49. 字母异位词分组(PYTHON)

给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。 示例: 输入: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”], 输出: [ [“ate”,“eat”,“tea”], [“nat”,“tan”], [“bat”] ] 说明: 所有输入均为小写字母。 不考虑答案输出的顺序。 第一次的代码,最后一个超时了。。。: sta...

weixin_44033136的博客 305

青龙人形机器人硬件平台设计全解析:从构型到控制系统的实战指南

本文深度解析了青龙人形机器人硬件平台的设计全流程,从需求分析、构型设计到核心系统实现。文章详细阐述了其43个自由度的仿生构型、高动态性下肢设计、灵巧作业系统以及分布式控制架构,并探讨了旋转关节与直线关节等关键技术选型,为开发者提供了从理论到实践的完整硬件设计指南。

weixin_29248313的博客 397

Python入门基础教程(非常详细)

Python是一门开源免费、通用型的脚本编程语言,它上手简单,功能强大,坚持「极简主义」。Python类库(模块)极其丰富,这使得Python几乎无所不能,不管是传统的Web开发、PC软件开发、Linux运维,还是当下火热的机器学习、大数据分析、网络爬虫,Python都能胜任。这套Python基础教程不是教科书,不会玩弄概念,而是力求口语化和通俗化,让读者尽快入门。如果有小朋友出于兴趣学习Python,请引导他阅读本教程的前半部分,这是基础,可以降低学习成本;...

Yuki1127918的博客 34万+

leetcode242题 题解 翻译 C语言版 Python版

242. Valid Anagram Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = "anagram", t = "nagaram", return true. s = "rat", t = "car", return fals

陈止风的博客 1102

苏州大学自动化考研842自动控制原理:手把手教你用胡寿松《自控》高效备考(附复试电工/电子/微机原理攻略)

本文详细解析苏州大学自动化考研842自动控制原理备考策略,以胡寿松《自动控制原理》为核心教材,提供三阶段高效学习路径和实战技巧。重点涵盖频域分析、根轨迹绘制等高频考点,并针对机电、光电、轨道三个学院的复试科目(电工电子技术、电子技术基础、微机原理与接口技术)给出差异化备考方案,助力考生全面提升笔试和面试表现。

weixin_42638178的博客 416

Python刷题:哈希表(三)

11.Find All Anagrams in a String     Given a string s and a non-empty string p, find all the start indices of p’s anagrams in s. Strings consists of lowercase English letters only and the length of b...

zhtysw的博客 1915

电阻基础知识及应用电路仿真(Multisim)

本文围绕硬件工程师必备的电阻知识,从基础知识、实操、应用仿真三方面做系统讲解:基础上明确电阻的核心参数、分类、串并联计算及核心特性;实操中详解贴片/色环电阻的阻值读取、贴片封装换算,重点区分0Ω电阻(贴装跳线,实现功能选通/调试等)与磁珠(高频吸噪)的差异及选型;应用上介绍限流、分压、上拉/下拉、阻抗匹配、过压/过流保护五大典型电路的设计公式、选型原则和实操要点,同时给出电阻通用的选型、测量和布线设计规范,形成完整的电阻应用知识体系。

XU157303764的博客 2804

Openclaw 附录B 常用Skills清单

核心作用支持URL、网页、PDF、图片、音频和YouTube视频的摘要与字幕提取,由OpenClaw创始人@steipete开发的官方内置Skill。安装⚠️说明:原推荐的summarize未在ClawHub验证到,YouTube字幕提取功能已内置于summarize中。使用场景“提取这个2小时Next.js教程视频的核心代码逻辑,按章节整理成学习笔记”推荐指数:⭐⭐⭐⭐。

Maynor的博客 870

LeetCode 242 Valid Anagram(有效字谜)(*)

翻译给定两个字符串s和t,写一个函数来确定是否t是s的字谜。例如, s = "anagram", t = "nagaram", 返回true s = "rat", t = "car", 返回false备注: 你可以假设字符串只包含小写字母。跟进: 如果输入包含unicode字符该怎么做?你的解决方案能够适应这种情况吗?原文Given two strings s and t, write a func

nomasp 2627

第182期 Claude Skills:深入探究Anthropic的智能体框架(一篇彻底讲透彻)

摘要: Claude Skills是Anthropic推出的智能体框架,通过连接外部工具和服务扩展Claude AI的功能,支持实时交互与任务执行。其核心架构包含技能路由器、注册表、执行引擎等模块,采用渐进式披露机制优化上下文管理。与MCP和A2A相比,Skills更注重模块化设计与Claude深度集成,通过SKILL.md文件定义可重复工作流程,兼具高效性和灵活性。开发者可创建自定义技能,遵循聚焦用户价值、安全隐私等最佳实践,实现AI能力的场景化拓展。该框架与MCP、A2A形成互补,共同推动智能体生态发展

全网120W+关注AI拉呱,专注人工智能以及科技前沿! 2146

LeetCode 242. Valid Anagram 题解

本题是字符串处理的基础问题。排序比较字符计数边界条件处理通过本题可以深入理解字符串处理的基本方法。

cannonjinx的博客 4414

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

242. Valid Anagram*

242. Valid Anagram* https://leetcode.com/problems/valid-anagram/ 题目描述 Given two strings s and t , write a function to determine if t is an anagram of s. Example 1: Input: s = "anagram", t = "nagaram" ...

珍妮的选择的博客 302

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

【LeetCode OJ 242】Valid Anagram

题目链接:https://leetcode.com/problems/valid-anagram/ 题目:Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = "anagram", t = "nagaram", return true

1391

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

leetcode 242: Valid Anagram

leetcode 242: Valid Anagram python java c++

西施豆腐渣 7651

Leetcode 242. Valid Anagram (python+cpp)

Leetcode 242. Valid Anagram题目解析: 题目 解析: python: class Solution: def isAnagram(self, s: str, t: str) -> bool: if len(s) != len(t): return False counts = collections.defaultdict(int) for i in rang

qq_37821701的博客 247

LeetCode 242. Valid Anagram 解题报告

LeetCode 242. Valid Anagram 解题报告

骆小坑的填坑之旅 742

LeetCode: 242. Valid Anagram

LeetCode: 242. Valid Anagram 题目描述 Given two strings s and t , write a function to determine if t is an anagram of s. Example 1: Input: s = "anagram", t = "nagaram" Output: true Example 2: Input: s = ...

杨领well的专栏 232

LeetCode 242. Valid Anagram 题解(C++)

Given two strings s and t, write a function to determine if t is an anagram of s.

prayyojay的博客 1665

LeetCode - 242. Valid Anagram

题目: Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = "anagram", t = "nagaram", return true. s = "rat", t = "car", return false. Note: You ma

抹茶小一的博客 546

【LeetCode & 剑指offer刷题】字符串题14:242. Valid Anagram (变位词系列)

【LeetCode &amp; 剑指offer刷题】字符串题14:242. Valid Anagram (变位词系列) 【LeetCode &amp; 剑指offer 刷题笔记】目录(持续更新中...) 242. Valid Anagram (变位词) Given two strings s and t , write a function...

技术改变世界 601

LeetCode 242. Valid Anagram C语言

Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = "anagram", t = "nagaram", return true. s = "rat", t = "car", return false. 首先是要两个字符串的长度想等; 然

xishirujin_的博客 622
上一篇: [LeetCode]169. Majority Element(多数元素)
下一篇: [LeetCode]504. Base 7(七进制)
whl_program
博客等级 码龄11年 101粉丝 279原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址