leetcode算法之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.

翻译:判断给定的两个字符串是否为打乱了顺序的同一个字符串

我的java实现:

import java.util.HashMap;
public class Solution {
    public boolean isAnagram(String s, String t) {
    	if(s.length() != t.length()){
    		return false;
    	}
    	char[] c1 = s.toCharArray();
    	char[] c2 = t.toCharArray();
    	HashMap<Character,Integer> map1 = new HashMap<Character,Integer>();
    	HashMap<Character,Integer> map2 = new HashMap<Character,Integer>();
    	for(int i=0;i < s.length();i++){
    		if(!map1.containsKey(c1[i])){
    			map1.put(c1[i], 1);
    		}else{
    			map1.put(c1[i], map1.get(c1[i])+1);
    		}
    		if(!map2.containsKey(c2[i])){
    			map2.put(c2[i], 1);
    		}else{
    			map2.put(c2[i], map2.get(c2[i])+1);
    		}
    	}
    	if(map1.entrySet().containsAll(map2.entrySet()) && map2.entrySet().containsAll(map1.entrySet())){
    		return true;
    	}
        return false;
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-10 19:33:26

leetcode算法之Valid Anagram的相关文章

【LeetCode】242. Valid Anagram (2 solutions)

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 strin

LeetCode算法题-Valid Perfect Square(Java实现-四种解法)

这是悦乐书的第209次更新,第221篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第77题(顺位题号是367).给定正整数num,写一个函数,如果num是一个完美的正方形,则返回True,否则返回False.例如: 输入:16 输出:true 输入:14 输出:false 注意:不要使用任何内置库函数,例如sqrt. 本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试. 02 第一种解法 暴力解法

【LeetCode算法】Valid Parentheses

LeetCode第20题 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed i

LeetCode算法01 Valid Parentheses

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]"

[LeetCode] NO. 242 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. [题目解析] 首先明确题意,需要判断组成两个字符串的所有字符是否完全相同,即

Leetcode题目: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 contain

【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 = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false. Note:You may assume the string contains

LeetCode OJ 之 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 conta

【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 = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false. Note:You may assume the string contains