`

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.

判断两个词是否为异位构词,我们可以借助计数排序算法的思想来解决,代码如下:
public class Solution {
    public boolean isAnagram(String s, String t) {
        if(s == null || t == null) return true;
        int[] result = new int[26];
        for(int i = 0; i < s.length(); i++) {
            result[s.charAt(i) - 'a'] ++;
        }
        for(int i = 0; i < t.length(); i++) {
            result[t.charAt(i) - 'a'] --;
        }
        for(int i = 0; i < result.length; i++) {
            if(result[i] != 0) return false;
        }
        return true;
    }
}
分享到:
评论

相关推荐

    valid anagram

    输入两个字符串,验证这两个字符串是否只是顺序颠倒

    cpp-算法精粹

    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浇花-LCSolutions:我的力扣解决方案

    leetcode 浇花力扣解决方案 简单的 #0001 - Two Sum #0007 - Reverse Integer #0009 - Palindrome ...Valid ...Valid Anagram #0243 - Shortest Word Distance #0246 - Strobogrammatic Number #0263 -

    LeetCode最全代码

    ...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:leetcode-java

    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

    leetcode答案-valid-anagram:检查目标字符串是否是源字符串的变位词

    "anagram", t = "nagaram" Output: true Example 2: Input: s = "rat", t = "car" Output: false 注意:您可以假设字符串仅包含小写字母。 跟进:如果输入包含 unicode 字符怎么办? 您将如何使您的解决方案适应这种...

    leetcode和oj-leetCode:尝试一些OJ

    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

    Algorithms-DataStructures:一个存储我所有算法和数据结构工作的存储库

    算法-数据结构这是一个存储库,用于存储我研究算法和数据结构的所有工作。 在下面,您将找到我为LeetCode或在线课程...LeetCode Valid Anagram.py这个问题要求给定两个字符串s和t,编写一个函数来确定t是否是s的字谜

    lrucacheleetcode-LeetcodeQAPython:LeetcodePython3练习题

    validAnagram (Python3) Leetcode 121 买卖股票的最佳时机 (Python3) Leetcode 122 买卖股票的最佳时机 II (Python3) Leetcode 146 LRU 缓存 O(1) :) Leetcode 20 有效括号 Leetcode 36 有效数独 Leetcode 62 唯一...

    CodingInterviewProblems:在编码面试之前要解决的一堆问题

    完成解决方案后,通过运行以下命令运行ruby测试: ruby test/&lt;nameofproblem&gt;.rb 例如: ruby test/valid_anagram_test.rb 这是在此项目中发现的问题的列表: 向右旋转数组(array_rotation.rb) 查找第一个非重复...

    codewars_python_solutions:Python中的CodeWars解决方案

    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网站上的题目代码

    leetcode 持续刷leetcode中... 欢迎大家交流,也欢迎star. 关于leetcode网站上的题目解法,语言选择的Java。后续会进行题目翻译和解法分析。 题目分析: Easy Leetcode 760 : Find Anagram ...Leetcode 794: Valid

Global site tag (gtag.js) - Google Analytics