(LeetCode)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.

Follow up:

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

Subscribe to see which companies asked this question

解题分析:

此题的意思很容易理解,意思就是给出两个字符串,然后判断是不是有一个混排成另一个字符串的。

确定是混排的话返回 True, 不正确的话返回 False.

思路是先将字符串转化为 list 然后进行排序,最后在进行比较,如果是有效的话,绝对是相等的。

# -*- coding:utf-8 -*-
__author__ = 'jiuzhang'
class Solution(object):
    def isAnagram(self, s, t):
        s_list = []
        t_list = []
        if len(s) != len(t):
            return False
        else:
            for i in range(len(s)):
                s_list.append(s[i])
                t_list.append(t[i])
            s_list.sort()
            t_list.sort()
            flag = 0
            for i in range(len(s_list)):
                if s_list[i] == t_list[i]:
                    flag += 1
                else:
                    flag += 0
            if flag == len(s_list):
                return True
            else:
                return False
时间: 2024-10-14 16:20:31

(LeetCode)Valid Anagram --- 有效的混排字符串的相关文章

php strlen mb_strlen计算中英文混排字符串长度

在php中常见的计算字符串长度的函数有:strlen和mb_strlen,下面是对这两个函数的比较说明(编码方式UTF8) 比较strlen和mb_strlen  当字符全是英文字符的时候,两者是一样.这里主要比较一下,中英文混排的时候,两个计算结果.(测试时编码方式是UTF8) <?php $str='中文a字1符'; echo strlen($str); echo '<br />'; echo mb_strlen($str,'UTF8'); //输出结果 //14 //6 ?>

[leetcode] Valid Anagram 、 Find All Anagrams in a String

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:

[LeetCode] 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] 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 onl

[leetcode]Valid Anagram解题报告 C语言

[题目] 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 cont

LeetCode Valid Anagram (简单题)

题意: 给出两个字符串s和t,判断串t是否为s打乱后的串. 思路: 如果返回的是true,则两个串的长度必定相等,所有字符出现的次数一样.那么可以统计26个字母的次数来解决,复杂度O(n).也可以排序后逐个比对,复杂度O(nlogn). 第一种方法: 1 class Solution { 2 public: 3 bool isAnagram(string s,string t) 4 { 5 if(s.size()!=t.size()) return false; 6 int cnt[26][2]

LeetCode Valid Palindrome 有效回文(字符串)

1 class Solution { 2 public: 3 bool isPalindrome(string s) { 4 if(s=="") return true; 5 if(s.length()==1) return true; //单个字符,对称 6 char *p,*q; 7 p=&s[0]; //p指向开头 8 q=&s[s.length()-1]; //q指向末尾 9 while(p!=q){ 10 //测试字符串里是否有字母或数字,若没有,则立刻返回

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

php实现中英文混排字符串截取

mb_strwidth($str, $encoding) 返回字符串的宽度 mb_strimwidth($str, $start, $width, $tail, $encoding) 按宽度截取字符串$str 要截取的字符串$start 从哪个位置开始截取,默认是0$width 要截取的宽度$tail 追加到截取字符串后边的字符串,常用的是 ...$encoding 要使用的编码