- 浏览: 174890 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
判断两个词是否为异位构词,我们可以借助计数排序算法的思想来解决,代码如下:
For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.
Note:
You may assume the string contains only lowercase alphabets.
判断两个词是否为异位构词,我们可以借助计数排序算法的思想来解决,代码如下:
public class Solution { public boolean isAnagram(String s, String t) { if(s == null || t == null) return true; int[] result = new int[26]; for(int i = 0; i < s.length(); i++) { result[s.charAt(i) - 'a'] ++; } for(int i = 0; i < t.length(); i++) { result[t.charAt(i) - 'a'] --; } for(int i = 0; i < result.length; i++) { if(result[i] != 0) return false; } return true; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 229Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 233You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 349Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 343Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 463Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 526Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 438Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 626Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 436The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 394Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 522Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 543Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 379All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 873Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 886Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 564Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 612Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 772Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 741You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 635For a undirected graph with tre ...
相关推荐
输入两个字符串,验证这两个字符串是否只是顺序颠倒
Valid Anagram Simplify Path Length of Last Word Isomorphic Strings Word Pattern 栈和队列 栈 Min Stack Valid Parentheses Longest Valid Parentheses Largest Rectangle in Histogram Evaluate Reverse Polish...
leetcode 浇花力扣解决方案 简单的 #0001 - Two Sum #0007 - Reverse Integer #0009 - Palindrome ...Valid ...Valid Anagram #0243 - Shortest Word Distance #0246 - Strobogrammatic Number #0263 -
...The number of questions is increasing recently. Here is the classification of all `468` questions. ...I'll keep updating for full summary and better solutions....|-----|---------------- | --------------- |...
Leetcode的ac是什么意思 LeetCodeInJava List #98 Validate Binary Search Tree ...Anagram #258 Add Digits #260 Single Number III #274 H-Index #283 Move Zeroes #292 Nim Game #318 Maximum P
"anagram", t = "nagaram" Output: true Example 2: Input: s = "rat", t = "car" Output: false 注意:您可以假设字符串仅包含小写字母。 跟进:如果输入包含 unicode 字符怎么办? 您将如何使您的解决方案适应这种...
leetcode 和 oj leetcode 尝试一些OJ。 412 Fizz Buzz 57.8% Easy 344 Reverse String 57.3% ...Valid Anagram 44.2% Easy 409 最长回文 44.1% Easy 169 Majority 元素 44.0% Easy 217. 包含 Easy Bi
算法-数据结构这是一个存储库,用于存储我研究算法和数据结构的所有工作。 在下面,您将找到我为LeetCode或在线课程...LeetCode Valid Anagram.py这个问题要求给定两个字符串s和t,编写一个函数来确定t是否是s的字谜
validAnagram (Python3) Leetcode 121 买卖股票的最佳时机 (Python3) Leetcode 122 买卖股票的最佳时机 II (Python3) Leetcode 146 LRU 缓存 O(1) :) Leetcode 20 有效括号 Leetcode 36 有效数独 Leetcode 62 唯一...
完成解决方案后,通过运行以下命令运行ruby测试: ruby test/<nameofproblem>.rb 例如: ruby test/valid_anagram_test.rb 这是在此项目中发现的问题的列表: 向右旋转数组(array_rotation.rb) 查找第一个非重复...
CodeWars Python解决...str , valid , data types 1个 hashmap , dict , data types 1个 str , set , data types 1个 str , format 1个 str , format , regex 2个 str , array , list , anagram 2个 st
leetcode 持续刷leetcode中... 欢迎大家交流,也欢迎star. 关于leetcode网站上的题目解法,语言选择的Java。后续会进行题目翻译和解法分析。 题目分析: Easy Leetcode 760 : Find Anagram ...Leetcode 794: Valid