本文是在学习中的总结,欢迎转载但请注明出处: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. 分布式缓存GemFire架构介绍

    1什么是GemFire GemFire是一个位于应用集群和后端数据源之间的高性能.分布式的操作数据(operational data)管理基础架构.它提供了低延迟.高吞吐量的数据共享和事件分发.Gem ...

  2. 【NPR】漫谈轮廓线的渲染

    写在前面 好久没写文章.最近在看<Real Time Rendering, third edition>这本书,看到了NPR这一章就想顺便记录下一些常见的轮廓线渲染的方法. 在非真实感渲染 ...

  3. memcached实战系列(二)memcached参数以及启动

    memcached启动的时候配置的参数也比较多.在这里我就做一个汇总,需要的时候直接查看参数以及参数的含义. 下面是参数的定义以及解释. 1.1.1. 参数说明 -d选项是启动一个守护进程 -m是分配 ...

  4. 03_dbcp数据源依赖jar包,DBCP中API介绍,不同过dbcp方式使用dbcp数据库连接池,通过配置文件使用dbcp数据库连接池

     DBCP数据源 使用DBCP数据源,需要导入两个jar包 Commons-dbcp.jar:连接池的实现 Common-pool.jar:连接池实现的依赖库. 导入mysql的jar包. DBC ...

  5. Ext JS 6开发实例(一)

    很久没写文章了,主要原因和大家差不多,都要为生活奔忙,搞了两个小项目.这两个小项目很凑巧,都可以使用Ext JS来开发,这正是练习使用Ext JS 6的好机会,自然不会错过. 很多读者可能会问,为什么 ...

  6. sharepoint adfs Adding Claims to an Existing Token Issuer in SharePoint 2010

    转载链接 http://www.theidentityguy.com/articles/2010/10/19/adding-claims-to-an-existing-token-issuer-i ...

  7. Android开发学习之路--传感器之初体验

    说到传感器,还是有很多的,有加速度啊,光照啊,磁传感器等等.当然android手机之所以称为智能手机,少不了这几款传感器的功劳了.下面就学习下了,这里主要学习光照,加速度和磁. 新建工程emSenso ...

  8. Win 10 下 android studio显示 Intel haxm无法安装,以及VT-X和hyper-x的冲突问题

               我 的电脑是神舟战神k650c i7 D4,处理器是Intel core i7 4710-MQ,系统是win 10的 我心血来潮想学习一下安卓开发,就首先安装了android s ...

  9. 编译GDAL使用最新的HDF库配置文件

    HDF库最新版本中的动态库以及目录结构都发生了变化,导致按照之前的博客进行编译GDAL时,会出问题.使用HDF4版本为HDF4-4.2.10,HDF5的版本为HDF5-1.8.12.两个库的目录结构如 ...

  10. Android的数字选择器NumberPicker-android学习之旅(三十七)

    我想说的话 今天晚上我依然在图书馆写博客,其实此刻我的没心激动而忐忑,因为明天就是足球赛的决赛,我作为主力球员压力很大,因对对方很强大,但是那又怎么样.so what...我不会停止写博客的 Numb ...