zoukankan      html  css  js  c++  java
  • 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 string contains only lowercase alphabets.

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

    判断是否是回文,即判断是否两个数组只是顺序不同,内容一致。

    记录第一个数组中每个字母出现次数,遍历第二个数组,遇到一个就减去一次该字母出现次数

    遍历出现次数的数组,全部为0则说明两个数组一样

    public class Solution {
        public bool IsAnagram(string s, string t) {
            if(s.Length != t.Length)
            {
                return false;
            }
            
            int[] count = new int[26];
            char[] sList = s.ToCharArray();
            char[] tList = t.ToCharArray();
            
            for(int i = 0; i < s.Length; i++)
            {
                count[(int)sList[i] - (int)'a']++;
            }
            
            for(int i = 0;i < t.Length; i++)
            {
                count[(int)tList[i] - (int)'a']--;
            }
            
            for(int i = 0; i < count.Count(); i++)
            {
                if(count[i] != 0)
                {
                    return false;
                }
            }
            
            return true;
        }
    }
  • 相关阅读:
    JSP第六周作业
    JSP第四次(2.0)
    JSP第四周
    软件测试课堂练习1
    5。建库,表,增删改查
    4.安卓练习
    2android九宫格
    第七周作业
    jsp第六周作业
    jsp第四周作业
  • 原文地址:https://www.cnblogs.com/Anthony-Wang/p/5076353.html
Copyright © 2011-2022 走看看