242 Valid Anagram 有效的字母异位词
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词。
例如,
s = "anagram",t = "nagaram",返回 true
s = "rat",t = "car",返回 false
注意:
假定字符串只包含小写字母。
提升难度:
输入的字符串包含 unicode 字符怎么办?你能能否调整你的解法来适应这种情况?
详见:https://leetcode.com/problems/valid-anagram/description/
字母异位词:两个字符串使用的小写字母个数一样,但是每个字母所处的位置不是全都一样。
Java实现:
class Solution {
public boolean isAnagram(String s, String t) {
if(s.length()!=t.length()){
return false;
}
int[] hash=new int[26];
for(int i=0;i<s.length();++i){
++hash[s.charAt(i)-'a'];
}
for(int i=0;i<t.length();++i){
if(--hash[t.charAt(i)-'a']<0){
return false;
}
}
return true;
}
}
C++实现:
class Solution {
public:
bool isAnagram(string s, string t) {
if(s.size()!=t.size())
{
return false;
}
int m[26]={0};
for(int i=0;i<s.size();++i)
{
++m[s[i]-'a'];
}
for(int j=0;j<t.size();++j)
{
if(--m[t[j]-'a']<0)
{
return false;
}
}
return true;
}
};
参考:https://www.cnblogs.com/grandyang/p/4694988.html
242 Valid Anagram 有效的字母异位词的更多相关文章
- 22. leetcode 242. Valid Anagram(由颠倒字母顺序而构成的字)
22. 242. Valid Anagram(由颠倒字母顺序而构成的字) Given two strings s and t, write a function to determine if t i ...
- 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 = & ...
- LeetCode 242. 有效的字母异位词(Valid Anagram)
242. 有效的字母异位词 LeetCode242. Valid Anagram 题目描述 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词. 示例 1: 输入: s ...
- [Swift]LeetCode242. 有效的字母异位词 | Valid Anagram
Given two strings s and t , write a function to determine if t is an anagram of s. Example 1: Input: ...
- C#LeetCode刷题之#242-有效的字母异位词(Valid Anagram)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4040 访问. 给定两个字符串 s 和 t ,编写一个函数来判断 ...
- 前端与算法 leetcode 242. 有效的字母异位词
目录 # 前端与算法 leetcode 242. 有效的字母异位词 题目描述 概要 提示 解析 解法一:哈希表 解法二:数组判断字符出现次数 解法三:转换字符串 算法 传入测试用例的运行结果 执行结果 ...
- Java实现 LeetCode 242 有效的字母异位词
242. 有效的字母异位词 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词. 示例 1: 输入: s = "anagram", t = " ...
- 【LeetCode】242. 有效的字母异位词
242. 有效的字母异位词 知识点:字符串:哈希表 题目描述 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词. 注意:若 s 和 t 中每个字符出现的次数都相同,则称 ...
- Leetcode 242.有效的字母异位词 By Python
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词. 示例 1: 输入: s = "anagram", t = "nagaram" ...
随机推荐
- JVM 总结
面试 java 虚拟机 jvm 基础 jvm Write Once Run EveryWhere >jar 包可以在任何兼容jvm上运行 >jvm 适配器 屏蔽掉底层差异 >内存管理 ...
- POJ 3469_Dual Core CPU
题意: N个模块可以在A,B两个核上运行,分别需要A[i]和B[i],模块之间需要传递数据,若两个模块在同一核上,则不需要花费,否则需要花费w[i].问最少需要花费多少? 分析: 用最小的费用将两个对 ...
- poj——2367 Genealogical tree
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6025 Accepted: 3969 ...
- HashSet源码分析1
import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class SetTest { pu ...
- oracle的processes和session最大限制
1.现象:oracle运行了一段时间后出现用户名连接不上,提示process已经达到最大值. 2.解决: --管理员身份登录 sqlplus / as sysdba --修改processes最大值, ...
- [Python] How to unpack and pack collection in Python?
It is a pity that i can not add the video here. As a result, i offer the link as below: How to unpa ...
- UVa10048_Audiophobia(最短路/floyd)(小白书图论专题)
解题报告 题意: 求全部路中最大分贝最小的路. 思路: 类似floyd算法的思想.u->v能够有另外一点k.通过u->k->v来走,拿u->k和k->v的最大值和u-&g ...
- Write a program that gives count of common characters presented in an array of strings..(or array of
转自出处 Write a program that gives count of common characters presented in an array of strings..(or arr ...
- YTU 2547: Repairing a Road
2547: Repairing a Road 时间限制: 1 Sec 内存限制: 128 MB 提交: 3 解决: 2 题目描述 You live in a small town with R b ...
- openstack horizon 学习(2) navigation、dashboard、panels
本章的主要内容是如何用horizon的navigation结构添加一个应用的面板. Horizon中提供了两种为应用添加panel的方法,一种是通过Pluggable Settings的方式,另一种是 ...