Description

Write a method anagram(s,t) to decide if two strings are anagrams or not.

Clarification

What is Anagram?

  • Two strings are anagram if they can be the same after change the order of characters.

Example

Given s = "abcd", t = "dcab", return true.
Given s = "ab", t = "ab", return true.
Given s = "ab", t = "ac", return false.

Challenge

O(n) time, O(1) extra space

解题:题目给定两个字符串,判断这两个字符串除了字符字符顺序不同外,是否相等。最容易想到的还是将字符排序,判断对位字符是否相等。不过在java中,要对字符串中的字符排序,只能转化成字符数组,那么就不满足challenge条件了。但是用其他方法(例如哈希表),代码就只能处理字母或者ASCII中的字符,有损通用性,没什么意思。贴一下排序方法的代码:

 public class Solution {
/**
* @param s: The first string
* @param t: The second string
* @return: true or false
*/
public boolean anagram(String s, String t) {
// write your code here
char[]arr_s = s.toCharArray();
char[]arr_t = t.toCharArray();
Arrays.sort(arr_s);
Arrays.sort(arr_t);
return String.valueOf(arr_s).equals(String.valueOf(arr_t));
}
}

158. Valid Anagram【LintCode by java】的更多相关文章

  1. 156. Merge Intervals【LintCode by java】

    Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...

  2. 212. Space Replacement【LintCode by java】

    Description Write a method to replace all spaces in a string with %20. The string is given in a char ...

  3. 177. Convert Sorted Array to Binary Search Tree With Minimal Height【LintCode by java】

    Description Given a sorted (increasing order) array, Convert it to create a binary tree with minimal ...

  4. 165. Merge Two Sorted Lists【LintCode by java】

    Description Merge two sorted (ascending) linked lists and return it as a new sorted list. The new so ...

  5. 173. Insertion Sort List【LintCode by java】

    Description Sort a linked list using insertion sort. Example Given 1->3->2->0->null, ret ...

  6. 172. Remove Element【LintCode by java】

    Description Given an array and a value, remove all occurrences of that value in place and return the ...

  7. 30. Insert Interval【LintCode by java】

    Description Given a non-overlapping interval list which is sorted by start point. Insert a new inter ...

  8. 155. Minimum Depth of Binary Tree【LintCode by java】

    Description Given a binary tree, find its minimum depth. The minimum depth is the number of nodes al ...

  9. 211. String Permutation【LintCode by java】

    Description Given two strings, write a method to decide if one is a permutation of the other. Exampl ...

随机推荐

  1. 为GRIDVIEW“删除”列添加确认对话框

    如何为gridview控件里的“删除”列添加一个确认对话框?网上众说纷纭,大致见到了三种解决方案,现归纳如下:1.继承Web.IO里的button控件,为其实现一个IPostback的接口用于回调,具 ...

  2. 增加oracle表空间

    查找用户对应的表空间 1.查询表空间物理文件路径select tablespace_name, file_id, file_name,round(bytes/(1024*1024),0) total_ ...

  3. 使用dbca命令静默卸载数据库

    1)     help查询dbca的选项 su - oracledbca -help dbca [-silent | -progressOnly | -customCreate] {<comma ...

  4. 竞赛题解 - NOIP2018 保卫王国

    \(\mathcal{NOIP2018}\) 保卫王国 - 竞赛题解 按某一个炒鸡dalao名曰 taotao 的话说: \(\ \ \ \ \ \ \ \ \ "一道sb倍增题" ...

  5. try...catch..finally..语句中,finally是否必须存在?作用是什么

    try { } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ } 1: ...

  6. NPOI读取Excel遇到的坑

    NPOI是POI的.NET版本.POI是用Java写成的库,能帮助用户在没有安装Office环境下读取Office2003-2007文件.NPOI在.NET环境下使用,能读写Excel/Word文件. ...

  7. 简单的HTTP协议

    HTTP 协议和 TCP/IP 协议族内的其他众多的协议相同,用于客户端和服务器之间的通信. 请求访问文本或图像等资源的一端称为客户端,提供资源响应的一端称为服务器端. 在两台计算机之间使用 HTTP ...

  8. 在 Ubuntu Linux 14.04 LTS 上安装php7

    首先添加php官方源 $ sudo add-apt-repository ppa:ondrej/php 然后更新源 $ sudo apt-get update 然后安装 $ sudo apt-get ...

  9. header()函数用处

    header() 函数向客户端发送原始的 HTTP 报头. 认识到一点很重要,即必须在任何实际的输出被发送之前调用 header() 函数(在 PHP 4 以及更高的版本中,您可以使用输出缓存来解决此 ...

  10. vowels_单元音

    vowels(美式): 单元音: [i]:需要用劲喊出类似于“yi”的四声,费力咧开嘴,单词eat.need.thief.meet. [?]:卷舌音,单词bird.her.worry.certain. ...