Lettocde_242_Valid Anagram

本文是在学习中的总结,欢迎转载但请注明出处: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;
	}

}

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

时间: 2024-10-04 22:53:24

Lettocde_242_Valid Anagram的相关文章

poj 2408 Anagram Groups(hash)

题目链接:poj 2408 Anagram Groups 题目大意:给定若干个字符串,将其分组,按照组成元素相同为一组,输出数量最多的前5组,每组按照字典序输出所 有字符串.数量相同的输出字典序较小的一组. 解题思路:将所有的字符串统计字符后hash,排序之后确定每组的个数并且确定一组中字典序最小的字符串.根据个数 以及字符串对组进行排序. #include <cstdio> #include <cstring> #include <vector> #include &

【09_242】Valid Anagram

Valid Anagram My Submissions Question Total Accepted: 43694 Total Submissions: 111615 Difficulty: Easy 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 t

leetcdoe Valid Anagram

题目连接 https://leetcode.com/problems/valid-anagram/ Valid Anagram Description 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.

【ACM从零开始】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 contains onl

[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. 分析: 判别“anagram”的条件: 1. 两个字符完全相等,两数完全为空:

poj1256 Anagram

Anagram Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18307   Accepted: 7452 Description You are to write a program that has to generate all possible words from a given set of letters. Example: Given the word "abc", your program s

[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 contains onl

242. Valid Anagram

Problem statement 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 s

242. Valid Anagram Add to List

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 on