【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 may assume the string contains only lowercase alphabets.
提示:
注意题目里提示了两个输入字符串中的字符均为小写字符,因此我们可以建立一个大小为26的字典(数组),其中的对应关系为{0:a, 1:b, ..., 25:z}。用它保存每一个字母出现的次数。
对s,将对应的数字+1,而对于t,将对应的数字-1。最后考察字典的所有元素是否都为零即可判断出每个字符出现的次数是否一致。
代码:
class Solution { public:bool isAnagram(string s, string t) {if (s.size() != t.size()) return false;int dic[26] = {0};for (int i = 0; i < s.size(); ++i) {dic[s[i] - 'a']++;}for (int i = 0; i < t.size(); ++i) {dic[t[i] - 'a']--;}for (int i = 0; i < 26; ++i) {if (dic[i] != 0) return false;}return true;} };
转载于:https://www.cnblogs.com/jdneo/p/4746078.html
【LeetCode】242. Valid Anagram相关推荐
- 【LeetCode】36. Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- [LeetCode] NO. 242 Valid Anagram
[题目] Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...
- 【LeetCode】1641. Count Sorted Vowel Strings(动态规划)
[LeetCode]1641. Count Sorted Vowel Strings(动态规划) Given an integer n, return the number of strings of ...
- HOT 100(61~80)【LeetCode】
HOT 100(61~80)[LeetCode] HHOT 100(61~80) 前言 推荐 207. 课程表[中等] 208. 实现 Trie (前缀树)[中等] 215. 数组中的第K个最大元素[ ...
- 【Leetcode】100. 相同的树
题目 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1/ \ / \2 3 2 3[1,2,3], [1 ...
- 【leetcode】85. Maximal Rectangle 0/1矩阵的最大全1子矩阵
1. 题目 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...
- 【leetcode】486. Predict the Winner
题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...
- 【leetcode】132. Palindrome Partitioning II
题目如下: 解题思路:本题是[leetcode]131. Palindrome Partitioning的升级版,要求的是求出最小cuts,如果用[leetcode]131. Palindrome P ...
- 【leetcode】86. Partition List
题目如下: Given a linked list and a value x, partition it such that all nodes less than x come before no ...
- 【Leetcode】103. 二叉树的锯齿形层次遍历
题目 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如: 给定二叉树 [3,9,20,null,null,15,7], 3 ...
最新文章
- 网络编程 UDP通信的过程 TCP通信过程 多线程文件上传
- POJ 3126 Prime Path(筛法,双向搜索)
- 第十五届全国大学生智能车竞赛线上竞赛方案(草案)
- 简明 Python 编程规范
- java日期时间的转化
- 用户类java,通过Java中的类和接口实现不同的用户类型
- Serverless化微服务架构实战
- python怎么编程输入坐标_python编程之API入门: (一)使用百度地图API查地理坐标...
- window.onload中动态获得img的高度
- 5000字 大数据时代读书笔记_大数据时代 读书笔记
- 微信处罚腾讯判定其滥用原创:不要惹我 我狠起来能铁锅炖自己!
- Vue基础1-如何创建一个vue实例
- Google正式收购SketchUp
- 计算机信息数字化基础1测验题,20春-计算机信息技术-章建民-1-中国大学mooc-题库零氪...
- 基于移动位置服务器,基于移动位置的服务系统及方法
- logistic逻辑回归公式推导及R语言实现
- Docker 使用快速入门
- 淘宝/天猫如何获得店铺的所有商品?
- 作为家庭娱乐衡量微型计算机基本技术指标,大学计算机 习题一..ppt
- 使用HTML制作简单的新闻页面
热门文章
- mysql 临时表 创建和插入
- 阶段1 语言基础+高级_1-3-Java语言高级_08-JDK8新特性_第1节 常用函数接口_8_常用的函数式接口_Supplier接口...
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_04 IO字节流_8_字节输入流_InputStream类FileInputStream...
- MyBatis笔记(一) 最简单的select
- 【Android】怎样烧写qcn文件
- sql语言的一大类 DML 数据的操纵语言
- 梦断代码最后4章读后感
- ESS And 迅雷5 让我不能上网
- idea关联mysql数据库具体操作
- jdk8,lambda表达(简化代码)