[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 = "rat", t = "car"
Output: false
题意:
验证变位词
何谓anagram?
思路:
用int[] map = new int[256]代表一个简化版的HashMap
挨个扫字符串s的每个字符,在map里标注++
挨个扫字符串t的每个字符,在map里标注--
根据valid anagram属性, map里最终应该都该为0。
代码:
class Solution {
public boolean isAnagram(String s, String t) {
if(s.length() != t.length()) return false;
int[] map = new int[256];
for(int i = 0 ; i < s.length(); i++){
map[s.charAt(i)]++;
map[t.charAt(i)]--;
}
for(int i : map){
if(i != 0){
return false;
}
}
return true;
}
}
[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: ...
- [LeetCode] Valid Anagram 验证变位词
Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = & ...
- 22. leetcode 242. Valid Anagram(由颠倒字母顺序而构成的字)
22. 242. Valid Anagram(由颠倒字母顺序而构成的字) Given two strings s and t, write a function to determine if t i ...
- LN : leetcode 242 Valid Anagram
lc 242 Valid Anagram 242 Valid Anagram Given two strings s and t, write a function to determine if t ...
- 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 = &q ...
- 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
Problem: Given two strings s and t, write a function to determine if t is an anagram of s. For examp ...
- (easy)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 = &q ...
- Java [Leetcode 242]Valid Anagram
题目描述: Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...
随机推荐
- sklearn.externals import joblib模块保存和下载使用模型的用法实例
#加载模块 from sklearn import datasets from sklearn.externals import joblib from sklearn.linear_model im ...
- TensorFlow系列专题(一):机器学习基础
- noip 2011 选择客栈
题目描述 丽江河边有n 家很有特色的客栈,客栈按照其位置顺序从 1 到n 编号.每家客栈都按照某一种色调进行装饰(总共 k 种,用整数 0 ~ k-1 表示),且每家客栈都设有一家咖啡店,每家咖啡店均 ...
- 并发工具类(三)控制并发线程的数量 Semphore
前言 JDK中为了处理线程之间的同步问题,除了提供锁机制之外,还提供了几个非常有用的并发工具类:CountDownLatch.CyclicBarrier.Semphore.Exchanger.Ph ...
- leetcode227
class Solution { public: stack<int> OPD; stack<char> OPR; int calculate(int num1, int nu ...
- NavitForMySql 破解工具使用
Navicat 11.0注册机使用教程: 1.右键-管理员权限运行注册机2.选择对应的产品3.点击“补丁”按钮,选择文件4.点击“生成”按钮,生成序列号,并保存下授权文件5.复制序列号,打开软件,在弹 ...
- nginx直接返回json
尝试配置nginx.conf之后,访问直接变成下载文件... 查阅之后,发现需要配置返回内容的格式. location ~ ^/get_json { default_type application/ ...
- ABAP-反调JCO服务
- ABAP-关于COMMIT WORK 和COMMIT WORK AND WAIT
转载:https://blog.csdn.net/champaignwolf/article/details/6925019 首先说明一点:更新是异步的,更新是由SAP中UPD1和UPD2两个进程执行 ...
- arguments.callee 属性 递归调用 & caller和callee的区别
arguments.callee 在函数内部,有两个特殊的对象:arguments 和 this.其中, arguments 的主要用途是保存函数参数, 但这个对象还有一个名叫 callee 的属 ...