【LeetCode 242】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.

题意:

  给2个字符串,判断他们是否相等,只是顺序不一样(专业术语叫:变位词)。

思路:

  很简单的一道题,方法有N多种,这里我使用一个大小为26的数组来解决(类似桶排序的思路),时间复杂度:O(n),空间复杂度:常数26。

C++:

 1 class Solution {
 2 public:
 3     bool isAnagram(string s, string t) {
 4
 5         int len = s.size();
 6         if(len != t.size())
 7             return false;
 8
 9         int a[26];
10         memset(a, 0, sizeof(a));
11
12         for(int i = 0; i < len; i++)
13         {
14             a[s[i] - ‘a‘]++;
15             a[t[i] - ‘a‘]--;
16         }
17
18         for(int i = 0; i < 26; i++)
19             if(a[i] != 0)
20                 return false;
21
22         return true;
23     }
24 };
时间: 2024-08-05 02:31:35

【LeetCode 242】Valid Anagram的相关文章

【leetcode系列】Valid Parentheses

很经典的问题,使用栈来解决,我这里自己实现了一个栈,当然也可以直接用java自带的Stack类. 自己实现的栈代码: import java.util.LinkedList; class StackOne { LinkedList<Object> data; int top; int maxSize; StackOne(int size) { // TODO Auto-generated constructor stub top = -1; maxSize = 100; data = new

【LeetCode OJ 242】Valid Anagram

题目链接:https://leetcode.com/problems/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", re

【LeetCode算法】Valid Parentheses

LeetCode第20题 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed i

【leetcode 简单】 第七十题 有效的字母异位词

给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词. 示例 1: 输入: s = "anagram", t = "nagaram" 输出: true 示例 2: 输入: s = "rat", t = "car" 输出: false 说明: 你可以假设字符串只包含小写字母. 进阶: 如果输入字符串包含 unicode 字符怎么办?你能否调整你的解法来应对这种情况? class Solution:

【LeetCode 229】Majority Element II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. 思路: [LeetCode 169]Majority Element 的拓展,这回要求的是出现次数超过三分之一次的数字咯,动动我们的大脑思考下,这样的数最多会存在几个呢,当然是2个嘛.因此,接着上一题的方

【LeetCode OJ】Sum Root to Leaf Numbers

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 # Definition for a  binary tree node # class TreeNode: #     def __init__(self, x): #         self.val = x #         self.left = No

【LeetCode OJ】Longest Consecutive Sequence

Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classical problem where we can reduce the running time by the help of hash table. By given a list of numbers, we can find the longest consecutive sequence b

【LeetCode OJ】Word Ladder I

Problem Link: http://oj.leetcode.com/problems/word-ladder/ Two typical techniques are inspected in this problem: Hash Table. One hash set is the words dictionary where we can check if a word is in the dictionary in O(1) time. The other hash set is us

【leetcode系列】String to Integer (atoi)

这个我就直接上代码了,最开始把"abc123"也算作合法的了,后来查了一下atoi的定义,把这种去掉了. public class Solution { public static int atoi(String inStr) { long result = 0L; /* * 网上查了一下,atoi函数的定义是如果第一个非空格字符存在,是数字或者正负号则开始做类型转换, * 之后检测到非数字(包括结束符\0)字符时停止转换,返回整型数.否则,返回零.可能的输入情况有: 1.空字符串 *