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. NopCommerce 3.4中移动端访问抛弃响应式布局

    在Nop3.4中,他抛弃了原来的xxx.Mobile.cshtml的这种写法,而是采用了响应式布局,并且把规则也给改了,你在后台配置不启用响应式布局,在前台你仍然不能写xxx.Mobile.cshtm ...

  2. ICT测试点是干什么的, 怎么设置!

    简单理解:ICT类似如万用表,只是把表笔换成了测试针.那么问题就简单了,一颗普通的RLC元件,都必须有两个测试点才能够测试,当然同一个网络共用的节点用一个测试点就可以了. 详细描述: PCB设计时要看 ...

  3. STM32平台SD卡的FatFS文件系统开发

    STM32平台SD卡的FatFS文件系统开发 系统平台: STM32系列的STM32F103ZE SPI方式与SD卡通信 SD上移植FatFS系统 1 FatFS文件系统 1.1 FatFS简介 Fa ...

  4. Many-to-many relationships in EF Core 2.0 – Part 4: A more general abstraction

    In the last few posts we saw how to hide use of the join entity from two entities with a many-to-man ...

  5. ios下引用MUI后input不能输入,Android端正常

    原因是mui框架的有个css样式 *{ -webkit-user-select: none; } 其作用是禁掉用户可以选中页面中的内容. 添加以下style样式即可 input{ -webkit-us ...

  6. 类似"音速启动"的原创工具简码"万能助手"在线用户数终于突破100了!

    原本只是开发出来方便自己的一个小工具,看到群友也喜欢,就随手分享了, 经过1个多月的自然积累,在线用户数终于突破100了,这增长速度实在让人泪奔~ 博客园的朋友如果看到,喜欢的话就拿去用吧, 万能助手 ...

  7. Cobbler实现自动化安装(下)--实现过程

    实验环境 [root@cobbler ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) [root@cobbler ~] ...

  8. Centos6.5 VM网络故障,可以Ping 通网关,无法上网或者访问其它网段

    首先查看cat /etc/sysconfig/network-scripts/ifcfg-eth0  配置是否正确 查看cat /etc/udev/rules.d/70-persistent-net. ...

  9. 论反向ajax

    在讨论反向ajax之前,说一下我大二写的一个聊天程序,通过前端ajax不断向服务器发送请求(通过http连接),服务器进行一次数据库查询,然后返回结果,断开与服务器的链接(绝大部分都是无用的结果)来维 ...

  10. php源码建博客1--搭建站点-实现登录页面

    主要: 站点搭建 实现登录页面 分析及改进 站点搭建 1)  在apache安装目录下: [conf\extra\httpd-vhosts.conf]加入站点配置 <VirtualHost *: ...