158. Valid Anagram【LintCode by java】
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】的更多相关文章
- 156. Merge Intervals【LintCode by java】
Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...
- 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 ...
- 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 ...
- 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 ...
- 173. Insertion Sort List【LintCode by java】
Description Sort a linked list using insertion sort. Example Given 1->3->2->0->null, ret ...
- 172. Remove Element【LintCode by java】
Description Given an array and a value, remove all occurrences of that value in place and return the ...
- 30. Insert Interval【LintCode by java】
Description Given a non-overlapping interval list which is sorted by start point. Insert a new inter ...
- 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 ...
- 211. String Permutation【LintCode by java】
Description Given two strings, write a method to decide if one is a permutation of the other. Exampl ...
随机推荐
- java两种反射的区别 - Class.forName()和ClassLoader.loadClass()
在理解这两种反射机制之前,需要弄清楚java类的加载机制. 装载:通过类的全限定名获取二进制字节流(二进制的class文件),将二进制字节流转换成方法区中的运行时数据结构,在内存中生成Java.lan ...
- CodeForces - 607B (记忆化搜索)
传送门: http://codeforces.com/problemset/problem/607/B Genos recently installed the game Zuma on his ph ...
- Vue教程:计算属性computed与侦听器watch(三)
计算属性computed 模板内的表达式非常便利,但是设计它们的初衷是用于简单运算的.在模板中放入太多的逻辑会让模板过重且难以维护.例如: <div id="example" ...
- 大数据框架-HDFS
HDFS:分布式文件系统,运行文件通过网络在多台主机分享的文件系统,分块写入(128M),适用于一次写入多次查询,不支持并发写(只能一块一块写),小文件不合适. nameNode(主节点,单个): 保 ...
- Dynamic Ambient Occlusion and Indirect Lighting
This sample was presented on the Nvida witesite, which detail a new idea to calculate the ambient oc ...
- Rem实现自适应布局
rem布局的目的是为了让我们可以用同一份代码,适应不同的移动终端(rem:就是css单位) 1.项目入口html文件<meta name="viewport" content ...
- Vue填坑(1)----通过vue-cli,认识vue-router
开始 首先,确保之前已经安装过 npm 和 nodejs(为了避免版本的问题,最好使用较新的版本). 全局安装 vue-cli : npm install -g vue-cli 新建文件夹 my-pr ...
- Zabbix——解决中文显示乱码
前提条件: 准备好上传工具,我用的是WinSCP 使用字体是微软雅黑,如果使用其他的更改名称即可. 更改Zabbix服务器的默认参数: vi /usr/share/zabbix/include/def ...
- 复习宝典之Spring
查看更多宝典,请点击<金三银四,你的专属面试宝典> 第六章:Spring Spring容器是Spring的核心,一切Spring bean都存储在Spring容器内,并由其通过IoC技术管 ...
- VMware10安装CentOS7
先去网上下载一个VMware的破解版或者激活版,安装配置这里就不介绍了自行下载安装,基本过程相当于windows下安装个软件而已. CentOS7镜像下载就下阿里云站点的这是链接 http://mir ...