leetcode242—Valid Anagram

Given two strings s and t , write a function to determine if t is an anagram of s.

Example 1:

Input: s = "anagram", t = "nagaram"
Output: true

Example 2:

Input: s = "rat", t = "car"
Output: 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?

想法:将字符串s和字符串t分别转成容器存储,然后按字符顺序排序。再比较两个容器是否相等。

class Solution {
public:
    bool isAnagram(string s, string t) {
        vector<char> st1;
        vector<char> st2;
        for(int i = 0 ; i < s.size() ; i++){
            st1.push_back(s.at(i));
        }
        sort(st1.begin(),st1.end());
        for(int j = 0 ; j < t.size() ; j++){
            st2.push_back(t.at(j));
        }
                          sort(st2.begin(),st2.end());
                          return st1 == st2;
    }
};

原文地址:https://www.cnblogs.com/tingweichen/p/9951659.html

时间: 2024-10-11 16:40:03

leetcode242—Valid Anagram的相关文章

leetCode242. Valid Anagram 合法的由颠倒字母顺序而构成的字 sort

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. Note:You may assume the

LeetCode242——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

LeetCode 242. 有效的字母异位词(Valid Anagram)

242. 有效的字母异位词 LeetCode242. Valid Anagram 题目描述 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词. 示例 1: 输入: s = "anagram", t = "nagaram" 输出: true 示例 2: 输入: s = "rat", t = "car" 输出: false 说明: 你可以假设字符串只包含小写字母. 进阶: 如果输入字符串包含 un

【09_242】Valid Anagram

Valid Anagram My Submissions Question Total Accepted: 43694 Total Submissions: 111615 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 t

leetcdoe Valid Anagram

题目连接 https://leetcode.com/problems/valid-anagram/ Valid Anagram Description 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

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:Valid Anagram

1.题目名称 Valid Anagram (易位构词) 2.题目地址 https://leetcode.com/problems/valid-anagram/ 3.题目内容 英文:Given two strings s and t, write a function to determine if t is an anagram of s. 中文:给出两个字符串,写一个函数判断t是否是s的易位构词 例如: s = "anagram", t = "nagaram",

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",