leetcode 242. Valid Anagram
来源:互联网 发布:编程是什么 编辑:程序博客网 时间:2024/06/07 05:46
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.
Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?
package leetcode;public class Valid_Anagram_242 {public boolean isAnagram(String s, String t) {if(s.length()!=t.length()){return false;}int n=s.length();int[] chars1=new int[128];int[] chars2=new int[128];char[] ar1=s.toCharArray();for(int i=0;i<n;i++){chars1[ar1[i]]++;}char[] ar2=t.toCharArray();for(int i=0;i<n;i++){chars2[ar2[i]]++;}for(int i=0;i<128;i++){if(chars1[i]!=chars2[i]){return false;}}return true;}public static void main(String[] args) {// TODO Auto-generated method stubValid_Anagram_242 v=new Valid_Anagram_242();System.out.println(v.isAnagram("anagram", "nagaram"));}}大神思路相同,解法更帅更简洁,我也要学着点。
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;}
阅读全文
0 0
- [leetcode] 242.Valid Anagram
- leetcode 242. Valid Anagram
- [leetcode] 242. Valid Anagram
- leetCode 242. Valid Anagram
- Leetcode 242. Valid Anagram
- 242. Valid Anagram LeetCode
- LeetCode 242. Valid Anagram
- LeetCode 242. Valid Anagram
- [LeetCode]242. Valid Anagram
- 【LeetCode】242. Valid Anagram
- leetcode 242. Valid Anagram
- leetcode 242. Valid Anagram
- LeetCode--242. Valid Anagram
- LeetCode *** 242. Valid Anagram
- #leetcode#242. Valid Anagram
- leetcode 242. Valid Anagram
- LeetCode 242. Valid Anagram
- 242.[LeetCode]Valid Anagram
- 手机设计公司最基本的六个部门:ID、MD、HW、SW、PM、Sourcing、QA
- 网络舆情安全体系的重要特性
- python-优矿-期权合成策略分析
- Android屏幕适配全攻略
- 用例建模的步骤
- leetcode 242. Valid Anagram
- AngularJS的学习--$on、$emit和$broadcast
- php递归函数详解
- Objective-C Autorelease Pool 的实现原理
- 图像缩放之双三次插值法
- angular实现模糊查询实例,后台就不需要单独写模糊查询的条件了
- 自定义View之PopupWindow的使用
- 快速上手 — Requests 2.10.0 文档
- 不安装oracle客户端 PLSQL11 64位 连接 ORACLE11g