首页 > 其他分享 >leetcode-242-easy

leetcode-242-easy

时间:2022-10-23 12:55:06浏览次数:63  
标签:count false int length easy 242 return true leetcode

Valid Anagram

Given two strings s and t, return true if t is an anagram of s, and false otherwise.

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Example 1:

Input: s = "anagram", t = "nagaram"
Output: true
Example 2:

Input: s = "rat", t = "car"
Output: false
Constraints:

1 <= s.length, t.length <= 5 * 104
s and t consist of lowercase English letters.
Follow up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case?

思路一: 刚开始想到用 xor 位运算,不过这题不能用。然后就是 map 的思路,记录字符出现的次数

public  boolean isAnagram(String s, String t) {
    if (s.length() != t.length()) return false;
    int[] count = new int[26];

    for (int i = 0; i < s.length(); i++) {
    count[s.charAt(i) - 'a']++;
    count[t.charAt(i) - 'a']--;
    }

    for (int i : count) {
    if (i != 0) return false;
    }

    return true;
    }

标签:count,false,int,length,easy,242,return,true,leetcode
From: https://www.cnblogs.com/iyiluo/p/16818372.html

相关文章

  • leetcode(31)字典树系列题目
    字典树,是一种是一种可以快速插入和搜索字符串的数据结构,有了它可以尽快的进行剪枝。将字典的信息全部转化到字典树上,只有出现在字典树上的路径,才应该被纳入到搜索空间里。......
  • leetcode-203-easy
    RemoveLinkedListElementsGiventheheadofalinkedlistandanintegerval,removeallthenodesofthelinkedlistthathasNode.val==val,andreturnth......
  • 数据结构 玩转数据结构 3-4 关于Leetcode的更多说明
    0课程地址https://coding.imooc.com/lesson/207.html#mid=13421 1重点关注1.1学习方法论1      自己花费了很多力气也解决不了的问......
  • 【重要】LeetCode 901. 股票价格跨度
    题目链接股票价格跨度注意事项使用单调栈代码classStockSpanner{public:StockSpanner(){this->stk.emplace(-1,INT_MAX);this->idx=-......
  • leetcode-283-easy
    MoveZeroes思路一:用left指针标记求解数组最大下标+1,初始化的时候是0,随着往右遍历,left会一步步扩大。最后把left右边的数都置为0。这题的关键在于left永远......
  • leetcode-231-easy
    PowerOfTwo思路一:观察2的n次方的二进制,都只有一位1bit,遍历即可publicbooleanisPowerOfTwo(intn){if(n<=0)returnfalse;intcount=0;......
  • LeetCode 1730. Shortest Path to Get Food
    原题链接在这里:https://leetcode.com/problems/shortest-path-to-get-food/题目:Youarestarvingandyouwanttoeatfoodasquicklyaspossible.Youwanttofind......
  • [LeetCode] 1768. Merge Strings Alternately
    Youaregiventwostrings word1 and word2.Mergethestringsbyaddinglettersinalternatingorder,startingwith word1.Ifastringislongerthantheot......
  • easyui文件限制格式
     文件后缀  easyui格式*.3gppaudio/3gpp,video/3gpp*.css text/css*.csv text/csv*.doc application/msword*.gif image/gif*.htm text/html*.html text/html*.jpeg......
  • 【leetcode_C++_哈希表_day5】242. 有效的字母异位词&&349. 两个数组的交集&&202.快乐
    C++知识补充:(不完全,仅针对本题用的知识点)1.C++类&对象关键字public确定了类成员的访问属性。在类对象作用域内,公共成员在类的外部是可访问的。您也可以指定类的成......