LeetCode 242 Valid Anagram(有效字谜)(*)

翻译

给定两个字符串s和t,写一个函数来确定是否t是s的字谜。

例如,
s = "anagram", t = "nagaram", 返回true
s = "rat", t = "car", 返回false

备注:
你可以假设字符串只包含小写字母。

跟进:
如果输入包含unicode字符该怎么做?你的解决方案能够适应这种情况吗?

原文

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.

Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?

分析

几乎秒写出来,不过不确定题目中的unicode那个能不能用,估计是不能把……因为我都不知道这个要怎么输入进去……以后再想办法把。

#include <iostream>
#include <algorithm>
using namespace std;

bool isAnagram(string s, string t) {
    sort(s.begin(), s.end());
    sort(t.begin(), t.end());
    return s == t;
}

int main() {
    string s = "anagram";
    string t = "nagaram";
    cout << isAnagram(s, t);
    return 0;
}

一开始看到翻译上给出的是“字谜”,题目中的意思我就不懂了,难道是这个谜语?然后另一个字符串是谜底?程序里又没有词库这种东西,怎么写……然后我去查了这个单词——“nagaram”,并不存在。

代码

class Solution {
public:
    bool isAnagram(string s, string t) {
        sort(s.begin(), s.end());
        sort(t.begin(), t.end());
        return s == t;
    }
};
时间: 2024-10-09 22:15:59

LeetCode 242 Valid Anagram(有效字谜)(*)的相关文章

leetcode:242 Valid Anagram-每日编程第八题

Valid Anagram Total Accepted: 42673 Total Submissions: 109227 Difficulty: Easy 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",

[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. 分析: 判别“anagram”的条件: 1. 两个字符完全相等,两数完全为空:

Leetcode 242 Valid Anagram pytyhon

题目: 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. 用到了python的sorted函数,原型:sorted(data, cmp=

[LeetCode] 242. Valid Anagram Java

题目: 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. 题意及分析:要求判断两个字符串是否由相同的字符组成.这里有两种方法,(1)统计

(easy)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. 解法:英文颠倒词,只要两个单词中,英文字母出现的次数相等即可. 代码如下: publi

Leetcode 242 Valid Anagram 字符串处理

字符串s和字符串t是否异构,就是统计两个字符串的a-z的字符数量是否一值 1 class Solution { 2 public: 3 4 bool isAnagram(string s, string t) { 5 int flgs[26] = {0};//统计s a-z的字符数量 6 for(int i = 0;i<s.size();++i){ 7 flgs[s[i] - 'a'] ++; 8 } 9 int flgt[26] = {0}; //统计t a-z的字符数量 10 for(int

[leetcode]242. Valid Anagram判断两个字符串是不是包含相同字符的重排列

/* 思路是判断26个字符在两个字符串中出现的次数是不是都一样,如果一样就返回true. 记住这个方法 */ if (s.length()!=t.length()) return false; int[] words = new int[26]; for (int i = 0; i < s.length(); i++) { words[s.charAt(i)-'a']++; words[t.charAt(i)-'a']--; } for (int i = 0; i < 26; i++) { i

242. Valid Anagram(C++)

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. 题目大意: 判断两字符串含有的元素是否相同.

【LeetCode】242. Valid Anagram (2 solutions)

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 strin