【ACM从零开始】LeetCode OJ-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.

题目大意:比较两个字符串是否有相同的字母个数组成。

解题思路:这里想到两种方法。

思路一:

建立一个表,记录字符串s里每个字符的个数,再与字符串t比对。

思路二:

将两个字符串的元素排序,然后比较大小。

首先是思路二的代码,提示TLE,可能是用冒泡排序导致时间复杂度过高,有空再修改

class Solution {
public:
    bool isAnagram(string s, string t) {
        char* sArray = new char(s.length()+1);
        char* tArray = new char(t.length()+1);
        sprintf(sArray ,"%s" , s.c_str());
        sprintf(tArray ,"%s" , t.c_str());
       
        BubbleSort(sArray,s.length()+1);
        BubbleSort(tArray,t.length()+1);
       
        s = sArray;
        t = tArray;
        return s==t;
    }
   
    void BubbleSort(char *p,int n)
    {
        for(int i = 0;i < n;i++)
        {
            for(int j = 0;j < n-i-1;j++)
            {
                if(p[j] > p[j+1])
                {
                    char tmp = p[j];
                    p[j] = p[j+1];
                    p[j+1] = tmp;
                }
            }
        }
    }
};

时间: 2024-10-10 07:05:12

【ACM从零开始】LeetCode OJ-Valid Anagram的相关文章

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][JavaScript]Valid Anagram

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

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

[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(有效字谜)(*)

翻译 给定两个字符串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

[LeetCode]50. 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 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