- 浏览: 172643 次
- 性别:
- 来自: 济南
文章分类
最新评论
有关异构词的题目,考察我们对字符串的处理能力,这里列举leetcode两道关于异构词的题目。
1,Valid Anagram
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.
Note:
You may assume the string contains only lowercase alphabets.
题目的意思是给定两个字符串,判断它们是否是异构词。
首先我们想到的方法是将字符串转换成字符数组,然后排序之后比较,如果两个新的字符串相同就返回true; 否则返回false;代码如下:
我们也可以采用一种类似于计数排序的方法来处理这个问题,先将一个字符串中的每个字符按照它们对应的不同的值来存入数组中,然后用第二个字符串中每个字符对应的值减去依次相减,如果数组中有的值不为零就返回false,如果全为0就返回true。代码如下:
2,Group Anagrams
Given an array of strings, group anagrams together.
For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"],
Return:
[
["ate", "eat","tea"],
["nat","tan"],
["bat"]
]
Note:
For the return value, each inner list's elements must follow the lexicographic order.
All inputs will be in lower-case.
题目的意思是给定一个字符串数组,将异构词分组,然后输出。我们借助哈希表,方法类似于第一题中的第一个方法,先排序,然后存放在哈希表中的key中,代码如下:
1,Valid Anagram
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.
Note:
You may assume the string contains only lowercase alphabets.
题目的意思是给定两个字符串,判断它们是否是异构词。
首先我们想到的方法是将字符串转换成字符数组,然后排序之后比较,如果两个新的字符串相同就返回true; 否则返回false;代码如下:
public class Solution { public boolean isAnagram(String s, String t) { char[] s1 = s.toCharArray(); char[] t1 = t.toCharArray(); Arrays.sort(s1); Arrays.sort(t1); s = new String(s1); t = new String(t1); if(s.equals(t)) return true; return false; } }
我们也可以采用一种类似于计数排序的方法来处理这个问题,先将一个字符串中的每个字符按照它们对应的不同的值来存入数组中,然后用第二个字符串中每个字符对应的值减去依次相减,如果数组中有的值不为零就返回false,如果全为0就返回true。代码如下:
public class Solution { public boolean isAnagram(String s, String t) { if(s.length() != t.length()) return false; int[] count = new int [128]; for (int i = 0; i < s.length(); i++) count[s.charAt(i) - '0'] ++; for (int j = 0; j < t.length(); j++) count[t.charAt(j) - '0'] --; for(int i = 0; i < count.length; i++) { if(count[i] != 0) return false; } return true; } }
2,Group Anagrams
Given an array of strings, group anagrams together.
For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"],
Return:
[
["ate", "eat","tea"],
["nat","tan"],
["bat"]
]
Note:
For the return value, each inner list's elements must follow the lexicographic order.
All inputs will be in lower-case.
题目的意思是给定一个字符串数组,将异构词分组,然后输出。我们借助哈希表,方法类似于第一题中的第一个方法,先排序,然后存放在哈希表中的key中,代码如下:
public class Solution { public List<List<String>> groupAnagrams(String[] strs) { HashMap<String, List<String>> hm = new HashMap<String, List<String>>(); for(int i = 0; i < strs.length; i++) { char[] temp = strs[i].toCharArray(); Arrays.sort(temp); String s = new String(temp); if(!hm.containsKey(s)) { hm.put(s, new ArrayList<String>()); } hm.get(s).add(strs[i]); } for(List<String> vals : hm.values()) { Collections.sort(vals); } return new ArrayList<List<String>>(hm.values()); } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 223Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 221You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 338Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 329Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 446Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 508Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 426Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 617Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 424The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 382Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 513Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 526Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 366All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 858Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 877Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 549Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 595Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 760Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 728You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 625For a undirected graph with tre ...
相关推荐
输入两个字符串,验证这两个字符串是否只是顺序颠倒
$ npm install compute-anagram-hash 要在浏览器中使用,请使用 。 用法 var createHash = require ( 'compute-anagram-hash' ) ; createHash( [arr] ) 创建一个哈希表。 var hash = createHash ( ) ; 要初始...
Anagram.java
this program calculate the anagram of a number among 1000-9999.
字谜查找器 克隆此仓库 运行以下命令 $ cd anagram/ $ python3 anagrams.py sample.txt 您将看到输出为 dear,more,post,pots,Read,Rome,stop
安娜 Java-Anagram-项目
步骤1 使用单元测试设置一个简单的项目
Anagram 是一个简单的 java 应用程序,它将在单词列表中找到 anagrams,并将它们打印到命令行。 该应用程序需要 gradle。 可以在找到安装 gradle 的说明。 如果您使用的是 linux,这将解决问题: sudo apt-get ...
字谜查找器 接受一段文本(希望是英语单词),并输出彼此字谜的单词组。 例如,“ apt”,“ tap”和“ pat”是彼此的字谜。...java -jar anagram/bin/anagram.jar 参数: 两个或两个以上“单词”: apt bu
We utilized Near-Infrared (NIR) spectroscopy to closely investigate the activation change in anterior prefrontal cortex (aPFC) during verbal anagram problem-solving and learning. We used a parametric ...
LEARN SD的Anagram项目测试两个单词是否互为字母使用Javascript,jQuery,HTML和rspec进行测试
node anagram.js 如果您有新的词典数据集,请以完全相同的顺序运行以下文件。 切记用文件wordlist.txt替换数据集 node index.js node normalize.js node anagram.js 我在美国科罗拉多州的亨廷湖编码了该程序。
IOS应用源码——schmidie-anagram-e27733b.rar
语言:English (United States) 解决任何字谜难题! 一个简单的应用程序,用于解决字谜拼字游戏。 当前,它仅支持单个单词字谜,但不久将添加多单词功能。
借助此扩展程序快速使您的字谜 您想做自己的名字或文字字谜吗? 使用此扩展名可以做到这一点。 它实际上是我们网站https://www.anagrammaker.online/的快捷方式。 你们两个都可以访问。 支持语言:English (UK)
字谜字谜实用程序功能的集合安装您可以通过将其添加到Cargo.toml文件中来安装此板条箱anagram = "0.3.0"例子use anagram :: {count, get_next, is_anagram, occurences};fn main () { // count how many anagrams ...
iOS游戏应用源代码——schmidie-anagram-e27733b.zip
字谜 一个简单的Java程序,可以生成字谜来解决。
相应的输出文件应分别命名为anagram1和anagram2 。 注意:请注意,此作业的目的是从基础实现任何必要的算法和数据结构。 因此,不要将库函数用于排序、哈希表等,它们必须从头开始实现。算法说明实现了一个哈希表,...