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 ...
随机推荐
- 在WinCE上播放声音、设置透明图片、系统音量 C#
记录一下用到的C#在WinCE平台上的相关技巧备查 1.C#在WinCE上实现透明图片 1 2 3 4 5 6 7 8 9 using System.Drawing.Imaging; pu ...
- LeetCode28.实现strStr() JavaScript
实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...
- javascript入门教程 (2)
这篇我就不铺垫和废话了,我们开始正式进入JS核心语法的学习… 首先我们从基础入手... 一. 基础语法 1.1 区分大小写 JS语法规定变量名是区分大小写的 比如: 变量名 learninpro 和变 ...
- 笔记: 对称加密算法的PKCS5 和 PKCS7 填充
PKCS #7 填充字符串由一个字节序列组成,每个字节填充该填充字节序列的长度. 假设,块的长度是 8, 数据长度是 5 数据:AA AA AA AA AA PKCS#7 填充 AA AA AA AA ...
- springboot(2.0以上) --数据源切换时报错
在进行数据源切换时spring.datasource.type类型根据源码所给的默认值修改后依然报错 先看源码:标色部分 , 就是springboot所给的数据源 , 正常来说只要在配置文件中修改 ...
- ASP.NET MVC4.0 后台获取不大前台传来的file
<td>选择图片</td> <td> <input type="file" id="uploadImg" name=& ...
- MySQL数据库的原理
点进去就是你历经千辛万苦找到的数据库的原理: https://www.cnblogs.com/smallyard/p/5626061.html
- Jupyter Notebook的魔法
在腾讯云上搭建了一个Jupyter Notebook 还是很好用的, 这里总结了一些看到的小技巧及快捷键使得我们更加快捷的完成工作. Notebook 与 Vim 有些类似,有两种键盘输入模式,即命令 ...
- ruby中的return方法及class实例方法的initialize方法
return是函数的返回值 class Mtring def initialize(str) @name = str end def aa ary = @name.split(/:/) return ...
- python教程(一)·python环境搭建
python的环境搭建总的来说分为两大步:下载.安装(废话@_@).在这里以windows为例(Linux通常内置了python,就算没有内置,相信Linux用户也非常清楚软件的安装方法) 第一步-下 ...