本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/48979767


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.

思路:

(1)题意为给定两个字符串,要求判定其中的一个字符串能否通过移位得到另一个字符串。

(2)该题考察的是两个字符串组成的字符是否完全一致。下面给出了三种不同的解题方法,方法一:将两个字符串转为字符数组,通过Arrays.sort()方法对字符数组进行排序,然后判断两字符数组组成的字符串是否完全一致来得到答案;方法二:借用map来保存其中一个字符串中的字符及其个数,然后遍历另一个字符串对应的字符数组,判定遍历到的字符是否存在map中,若不存在返回false,若存在,则当前字符在map中的值减1,遍历完即得到结果;方法三:借用一个整形数组来实现,该方法效率最好,且容易理解。由于a~Z对应的ASCII码值小于256,即创建一个256大小的数组即可,将其中一个字符串对应的字符存入数组中,数组下标为字符对应的ASCII码值,对应的值为当前字符的个数,然后遍历另一个字符串对应的字符数组,判断遍历得到的字符在整形数组中的值是否为0,若为0则返回false,否则将该字符对应的值减1,遍历完即得结果。

(3)详情见下方代码。希望本文对你有所帮助。

算法代码实现如下:

package leetcode;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author liqqc
 *
 */
public class Valid_Anagram {

	// use api method
	public static boolean isAnagram(String s, String t) {
		if (s == null || t == null)
			return false;

		if (s.trim().equals(t.trim()))
			return true;

		if (s.length() != t.length())
			return false;

		char[] charArray = s.toCharArray();
		char[] charArray2 = t.toCharArray();
		Arrays.sort(charArray);
		Arrays.sort(charArray2);

		return new String(charArray).equals(new String(charArray2));
	}

	// use map
	public static boolean isAnagram2(String s, String t) {
		if (s == null || t == null)
			return false;

		if (s.trim().equals(t.trim()))
			return true;

		if (s.length() != t.length())
			return false;

		char[] charArray = s.toCharArray();
		char[] charArray2 = t.toCharArray();

		Map<Character, Integer> map = new HashMap<Character, Integer>();
		for (Character c : charArray) {
			if (!map.containsKey(c)) {
				map.put(c, 1);
			} else {
				map.put(c, map.get(c) + 1);
			}
		}

		for (Character c : charArray2) {
			if (!map.containsKey(c)) {
				return false;
			} else {
				if (map.get(c) <= 0) {
					return false;
				} else {
					map.put(c, map.get(c) - 1);
				}
			}
		}

		return true;
	}

	// use array
	public static boolean isAnagram3(String s, String t) {

		if (s == null || t == null)
			return false;

		if (s.length() != t.length())
			return false;

		int[] arr = new int[256];

		for (char c : s.toCharArray()) {
			if (arr[c] == 0) {
				arr[c] = 1;
			} else {
				arr[c] = arr[c] + 1;
			}
		}

		for (char c : t.toCharArray()) {
			if (arr[c] == 0) {
				return false;
			} else {
				arr[c] = arr[c] - 1;
			}
		}

		return true;
	}

}

Leetocde_242_Valid Anagram的更多相关文章

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

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

  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 = &q ...

  3. 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 ...

  4. 【09_242】Valid Anagram

    Valid Anagram My Submissions Question Total Accepted: 43694 Total Submissions: 111615 Difficulty: Ea ...

  5. 【leetcode❤python】242. Valid Anagram

    class Solution(object):    def isAnagram(self, s, t):        if sorted(list(s.lower()))==sorted(list ...

  6. 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 ...

  7. (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 ...

  8. 【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. paypal的IPN机制

    paypal对接时发现有这么一个机制,看起来还不错,起到了防止篡改欺诈行为,保证了通信的安全性,但会增加几次通信.

  2. Android控制软键盘的弹出和隐藏

    弹出软键盘 前提:必须要有一个可以编辑的控件(EditText),并且当前已经获取焦点 /** * 弹出软键盘 */ public void openKeyboard(View view) { // ...

  3. 复习java逻辑---实现猜数字游戏

    package shopping; import java.util.*; public class SendGiftMenu { public static void main(String[] a ...

  4. unix下快速混淆源代码

    只能算雕虫小技,但可以快速简单的做混淆,如下: #vapyhqr <fgqvb.u> #vapyhqr <fgqyvo.u> #vapyhqr <fgqobby.u> ...

  5. 安卓java.lang.IllegalArgumentException: The observer is null.解决方案

    刚刚在调试自己的APP项目的时候报错java.lang.IllegalArgumentException: The observer is null.,而之前是可以运行通过,所以百思不得其解,后来在网 ...

  6. (一一五)利用NSKeyedArchiver实现任意对象转为二进制

    [应用背景] 在数据库中存储数据时,如果对象过于复杂,又不必要创建复杂的表,可以直接把整个对象转化为二进制存入数据库字段,然后取出后再还原即可. [实现方法] 在PHP中,使用序列化和反序列化可以实现 ...

  7. Android优化之ArrayMap

    ArrayMap的介绍 官方对ArrayMap也有说明:它不是一个适应大数据的数据结构,相比传统的HashMap速度要慢,因为查找方法是二分法,并且当你删除或者添加数据时,会对空间重新调整,在使用大量 ...

  8. Android的actionBar的菜单使用-android学习之旅(四十三)

    ActionBar简介 ActionBar是3.0以后加入的新特性,可用于在顶部显示应用名称和标题.初次之外右边还可以显示活动项. ActionBar应用 ActionBar用法 <?xml v ...

  9. 如何彻底的删除MySQL数据库(图文教程)

    最近有个小课题数据库使用Mysql,提前写一下Mysql作为复习. 第一步当然是要看如何卸载Mysql,因为安装之前要清理掉一切与Mysql有关的数据,否则后边安装失败. 以下操作以Window7操作 ...

  10. Java数组的应用2:数组的最大,最小,求和,平均值,倒置

    import java.util.Scanner; public class HelloWorld { public static void main(String[] args){ // Scann ...