解题思路:该题意思即是两个字符串是否具有相同的字符组成的串,首先记录第一个字符串出现的字符及其个数,然后与第二个字符串想比较。(另一个思路:将两个字符串分别排序,不过耗时。)
Java代码实现:
public class Solution {
public boolean isAnagram(String s, String t) {
if(s.length()!=t.length()) return false;
Map<String,Integer> map =new HashMap<String,Integer>();
for(int i=0;i<s.length();i++){
String str=String.valueOf(s.charAt(i));
if(map.containsKey(str)) map.put(str, map.get(str)+1);
else map.put(str, 1);
}
for(int i=0;i<t.length();i++){
String str=String.valueOf(t.charAt(i));
if(map.containsKey(str)) {
map.put(str, map.get(str)-1);
if(map.get(str)==0) map.remove(str);
}
else map.put(str, 1);
}
if(map.isEmpty()) return true;
else return false;
}
}
原题地址:
https://leetcode.com/problems/valid-anagram/
版权声明:本文为博主原创文章,未经博主允许不得转载。
分享到:
相关推荐
输入两个字符串,验证这两个字符串是否只是顺序颠倒
Valid Anagram Simplify Path Length of Last Word Isomorphic Strings Word Pattern 栈和队列 栈 Min Stack Valid Parentheses Longest Valid Parentheses Largest Rectangle in Histogram Evaluate Reverse Polish...
leetcode 浇花力扣解决方案 简单的 #0001 - Two Sum #0007 - Reverse Integer #0009 - Palindrome ...Valid ...Valid Anagram #0243 - Shortest Word Distance #0246 - Strobogrammatic Number #0263 -
...The number of questions is increasing recently. Here is the classification of all `468` questions. ...I'll keep updating for full summary and better solutions....|-----|---------------- | --------------- |...
Leetcode的ac是什么意思 LeetCodeInJava List #98 Validate Binary Search Tree ...Anagram #258 Add Digits #260 Single Number III #274 H-Index #283 Move Zeroes #292 Nim Game #318 Maximum P
"anagram", t = "nagaram" Output: true Example 2: Input: s = "rat", t = "car" Output: false 注意:您可以假设字符串仅包含小写字母。 跟进:如果输入包含 unicode 字符怎么办? 您将如何使您的解决方案适应这种...
leetcode 和 oj leetcode 尝试一些OJ。 412 Fizz Buzz 57.8% Easy 344 Reverse String 57.3% ...Valid Anagram 44.2% Easy 409 最长回文 44.1% Easy 169 Majority 元素 44.0% Easy 217. 包含 Easy Bi
算法-数据结构这是一个存储库,用于存储我研究算法和数据结构的所有工作。 在下面,您将找到我为LeetCode或在线课程...LeetCode Valid Anagram.py这个问题要求给定两个字符串s和t,编写一个函数来确定t是否是s的字谜
validAnagram (Python3) Leetcode 121 买卖股票的最佳时机 (Python3) Leetcode 122 买卖股票的最佳时机 II (Python3) Leetcode 146 LRU 缓存 O(1) :) Leetcode 20 有效括号 Leetcode 36 有效数独 Leetcode 62 唯一...
完成解决方案后,通过运行以下命令运行ruby测试: ruby test/<nameofproblem>.rb 例如: ruby test/valid_anagram_test.rb 这是在此项目中发现的问题的列表: 向右旋转数组(array_rotation.rb) 查找第一个非重复...
CodeWars Python解决...str , valid , data types 1个 hashmap , dict , data types 1个 str , set , data types 1个 str , format 1个 str , format , regex 2个 str , array , list , anagram 2个 st
leetcode 持续刷leetcode中... 欢迎大家交流,也欢迎star. 关于leetcode网站上的题目解法,语言选择的Java。后续会进行题目翻译和解法分析。 题目分析: Easy Leetcode 760 : Find Anagram ...Leetcode 794: Valid