鱼C论坛

 找回密码
 立即注册
查看: 2651|回复: 0

[学习笔记] leetcode 242. Valid Anagram

[复制链接]
发表于 2019-9-12 11:56:10 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. Given two strings s and t , write a function to determine if t is an anagram of s.

  2. Example 1:

  3. Input: s = "anagram", t = "nagaram"
  4. Output: true
  5. Example 2:

  6. Input: s = "rat", t = "car"
  7. Output: false
  8. Note:
  9. You may assume the string contains only lowercase alphabets.

  10. Follow up:
  11. What if the inputs contain unicode characters? How would you adapt your solution to such case?
复制代码

  1. class Solution {
  2.     public boolean isAnagram(String s, String t) {
  3.         
  4.         if(s.length() == 0 && t.length() == 0) return true;
  5.         
  6.         if(s.length() == 0 || t.length() == 0) return false;
  7.         
  8.         if(s.length() != t.length()) return false;
  9.         
  10.         char[] s1 = s.toCharArray();
  11.         char[] t1 = t.toCharArray();
  12.         
  13.         int[] res = new int[256];
  14.         
  15.         for(int i = 0; i< s1.length; i++){
  16.             
  17.             res[s1[i]]++;
  18.         }
  19.         
  20.         for(int i = 0; i< t1.length; i++){
  21.             
  22.             if(res[t1[i]]-- == 0) return false;
  23.         }
  24.         return true;
  25.     }
  26. }
复制代码

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-8 15:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表