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

Example 1:

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

Example 2:

Input: s = "rat", t = "car"
Output: 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?

给2个字符串s和t,写一个判断二者是否为变位词的函数。

解法1:利用HashMap记录s中出现的字母和数量,然后在用t的字母和数量来验证。

解法2:把两个string变为char array,对两个array进行排序,然后比较是否一样。

Java:

class Solution {
public boolean isAnagram(String s, String t) {
HashMap<Character, Integer> map = new HashMap<>(); // first time: store each s char and occurrence into map
for(int i=0; i<s.length(); i++) {
char sChar = s.charAt(i);
map.put(sChar, map.getOrDefault(sChar, 0) + 1);
}
// second time: compare t char with map to see match or not
for(int i=0; i<t.length(); i++) {
char tChar = t.charAt(i); if(!map.containsKey(tChar))
return false; if(map.get(tChar) == 1)
map.remove(tChar);
else
map.put(tChar, map.get(tChar) - 1); } return map.size() == 0 ? true : false;
}
} 

Java:

public class Solution {
public boolean isAnagram(String s, String t) {
int[] alphabet = new int[26];
for (int i = 0; i < s.length(); i++) alphabet[s.charAt(i) - 'a']++;
for (int i = 0; i < t.length(); i++) alphabet[t.charAt(i) - 'a']--;
for (int i : alphabet) if (i != 0) return false;
return true;
}
}  

Python:  T: O(n)  S: O(1)

class Solution:
def isAnagram(self, s, t):
if len(s) != len(t):
return False count = {} for c in s:
if c.lower() in count:
count[c.lower()] += 1
else:
count[c.lower()] = 1 for c in t:
if c.lower() in count:
count[c.lower()] -= 1
else:
count[c.lower()] = -1
if count[c.lower()] < 0:
return False return True

Python: wo

class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
a = [0] * 256
for i in s:
a[ord(i)] += 1 b = [0] * 256
for j in t:
b[ord(j)] += 1 return a == b   

Python:  T: O(n)  S: O(1)

class Solution:
def isAnagram3(self, s, t):
if len(s) != len(t):
return False
count = collections.defaultdict(int)
for c in s:
count[c] += 1
for c in t:
count[c] -= 1
if count[c] < 0:
return False
return True

Python:

def isAnagram1(self, s, t):
dic1, dic2 = {}, {}
for item in s:
dic1[item] = dic1.get(item, 0) + 1
for item in t:
dic2[item] = dic2.get(item, 0) + 1
return dic1 == dic2  

Python:  T: O(nlogn)  S: O(n)

class Solution:
def isAnagram(self, s, t):
return sorted(s) == sorted(t)

C++:

class Solution {
public:
bool isAnagram(string s, string t) {
if (s.size() != t.size()) return false;
int m[26] = {0};
for (int i = 0; i < s.size(); ++i) ++m[s[i] - 'a'];
for (int i = 0; i < t.size(); ++i) {
if (--m[t[i] - 'a'] < 0) return false;
}
return true;
}
};

    

All LeetCode Questions List 题目汇总

  

  

[LeetCode] 242. Valid Anagram 验证变位词的更多相关文章

  1. [leetcode]242. Valid Anagram验证变位词

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

  2. [LeetCode] Valid Anagram 验证变位词

    Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = & ...

  3. 22. leetcode 242. Valid Anagram(由颠倒字母顺序而构成的字)

    22. 242. Valid Anagram(由颠倒字母顺序而构成的字) Given two strings s and t, write a function to determine if t i ...

  4. LN : leetcode 242 Valid Anagram

    lc 242 Valid Anagram 242 Valid Anagram Given two strings s and t, write a function to determine if t ...

  5. LeetCode 242. Valid Anagram (验证变位词)

    Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...

  6. Leetcode 242. Valid Anagram(有效的变位词)

    Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = & ...

  7. LeetCode 242 Valid Anagram

    Problem: Given two strings s and t, write a function to determine if t is an anagram of s. For examp ...

  8. (easy)LeetCode 242.Valid Anagram

    Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...

  9. Java [Leetcode 242]Valid Anagram

    题目描述: Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...

随机推荐

  1. ARTS-week7

    Algorithm 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. Two Sum 编写一个 SQL 查询,满足条件:无论 ...

  2. jq function return value

    所有 JS  函数 都会返回值 假如 没有 return  则返回 undefined

  3. Codeforces Round #605 (Div. 3) B. Snow Walking Robot(构造)

    链接: https://codeforces.com/contest/1272/problem/B 题意: Recently you have bought a snow walking robot ...

  4. Java字符串之间拼接时,如果有null值,则会直接拼接上null

    package com.fgy.demo; public class demo06 { public static void main(String[] args) { String str1 = & ...

  5. LeetCode 1004. Max Consecutive Ones III

    原题链接在这里:https://leetcode.com/problems/max-consecutive-ones-iii/ 题目: Given an array A of 0s and 1s, w ...

  6. P4848 崂山白花蛇草水

    题意:支持修改的矩形第 \(k\) 大. 题解:动态开点权值线段树 套 Kd-tree. 然后也没什么难的但就是写不对...调了两天才调出来然后发现跑的巨慢,于是又%了一发Claris'题解,跑的真快 ...

  7. WinDbg命令窗口的使用

    调试器命令窗口是windbg中的主要调试信息窗口.可以在此窗口中输入调试程序命令并查看命令输出.Windbg的命令窗口是我们进行调试时,主要打交道的窗口.界面如下 对于windbg,“调试器命令窗口” ...

  8. 数组(定义、遍历、冒泡排序、合并和Join 方法)

    一.数组的定义 1.理解:数组指一组数据,有序的数据,可以一次性存储多个数据,将多个元素(通常统一类型)按照一定的顺序排列放到一个集合里 2.通过构造函数创建数组: var 数组名=new Arrar ...

  9. CSS行内块元素(内联元素)

    一.典型代表 input img 二.特点: 在一行上显示 可以设置宽高 <style type="text/css"> img{ width: 300px; /* 顶 ...

  10. 多项式总结&多项式板子

    多项式总结&多项式板子 三角/反三角是不可能放的(也不可能真香的 多项式乘法(DFT,FFT,NTT,MTT) 背板子 前置知识:泰勒展开 如果\(f(x)\)在\(x_0\)处存在\(n\) ...