Leetcode 242. Valid Anagram-判断是否是 相同字母异序词

Posted 二十六画生的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 242. Valid Anagram-判断是否是 相同字母异序词相关的知识,希望对你有一定的参考价值。

Given two strings s and t, return true if t is an anagram of s, and false otherwise.

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Example 1:

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

Example 2:

Input: s = "rat", t = "car"
Output: false

Constraints:

  • 1 <= s.length, t.length <= 5 * 104
  • s and t consist of lowercase English letters.

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

Middle可见:Leetcode 49. Group Anagrams--将变位词组合在一起-把相同字母异序词 分组_二十六画生的博客的博客-CSDN博客

package com.string;

import java.util.Arrays;

/**
 * @Author you guess
 * @Date 2022/4/8 22:05
 * @Version 1.0
 * @Desc 给定 s t 判断是否是  相同字母异序词
 */
public class Leetcode_242_ValidAnagram 

    /**
     * 方法1: c - 'a',遍历s把指定下标的元素加1 ,遍历t把指定下标的元素减1,遍历数组,只要有一位不为0则返回false
     * Runtime: 5 ms, faster than 65.98% of Java online submissions for Valid Anagram.
     * Memory Usage: 43 MB, less than 81.66% of Java online submissions for Valid Anagram.
     *
     * @param s
     * @param t
     * @return
     */
    public boolean isAnagram1(String s, String t) 
        if (s.length() != t.length()) 
            return false;
        

        //不可是Integer[] bArr = new Integer[10];初始化元素都是null
        int[] arr = new int[26];//初始化元素都是0
        for (int i = 0; i < s.length(); i++) 
            arr[s.charAt(i) - 'a']++;
        
        for (int i = 0; i < t.length(); i++) 
            arr[t.charAt(i) - 'a']--;
        
        for (int i = 0; i < arr.length; i++) 
            if (arr[i] != 0) return false;
        
        return true;
    


    /**
     * 方法2: char c,arr[c],遍历s把指定下标的元素加1 ,遍历t把指定下标的元素减1,遍历数组,只要有一位不为0则返回false
     * Runtime: 2 ms, faster than 98.98% of Java online submissions for Valid Anagram.
     * Memory Usage: 42.4 MB, less than 90.39% of Java online submissions for Valid Anagram.
     *
     * @param s
     * @param t
     * @return
     */
    public boolean isAnagram2(String s, String t) 
        if (s.length() != t.length()) 
            return false;
        
        int[] arr = new int[123];//a-z 97-122
        for (int i = 0; i < s.length(); i++) 
            arr[s.charAt(i)]++;//作用与下一行代码一致,把指定下标的元素加1
            //arr[(int)s.charAt(i)]++;
        
        for (int i = 0; i < t.length(); i++) 
            arr[t.charAt(i)]--;
            //arr[(int)t.charAt(i)]--;
        
        for (int i = 0; i < arr.length; i++) 
            if (arr[i] != 0) return false;
        
        return true;
    


    /**
     * 方法3:方法2的优化,遍历t时可以直接判断,不用最后遍历一遍结果数组
     * <p>
     * Runtime: 3 ms, faster than 92.89% of Java online submissions for Valid Anagram.
     * Memory Usage: 42.3 MB, less than 92.47% of Java online submissions for Valid Anagram.
     *
     * @param s
     * @param t
     * @return
     */
    public boolean isAnagram(String s, String t) 
        if (s.length() != t.length()) 
            return false;
        
        int[] arr = new int[123];//a-z 97-122
        for (int i = 0; i < s.length(); i++) 
            arr[s.charAt(i)]++;//作用与下一行代码一致,把指定下标的元素加1
            //arr[(int)s.charAt(i)]++;
        
        for (int i = 0; i < t.length(); i++) 
            if (arr[t.charAt(i)] > 0) 
                arr[t.charAt(i)]--;
             else 
                return false;
            
        
        return true;
    


    public static void main2(String[] args) 
        Leetcode_242_ValidAnagram main = new Leetcode_242_ValidAnagram();
//        System.out.println(main.isAnagram("afcv", "cvfa"));//true
//        System.out.println(main.isAnagram("afcv", "cvfad"));//false
        System.out.println(main.isAnagram("afcvb", "cvfad"));//false
    

    public static void main(String[] args) 
        int[] crr = new int[200];
        char a = 'a';
        System.out.println(crr[a]);//0
        System.out.println(crr[a]--);//0
        System.out.println(crr[a] + 2);//1

        

        int[] arr = new int[10];//初始化正确

        System.out.println(arr[0]);//0
        System.out.println(arr[0] + 1);//1
        System.out.println(arr[0] - 1);//-1
        System.out.println('b' - 'a');//1

        Integer[] bArr = new Integer[10];//初始化正确
        System.out.println(bArr[0]);//null
        //System.out.println(bArr[0]++);//NullPointerException
        //System.out.println(bArr[0]--);//NullPointerException

        Integer[] cArr = new Integer[]1, 2;//初始化正确
        //Integer[] dArr = new Integer[2]1,2;//报错

        

        char c = 'c';
        System.out.println(c);//c
        System.out.println((int) c);//99
        int cint = c;
        System.out.println(cint);//99

        

        String s = "askcbd";

        char[] sarr = s.toCharArray();
        System.out.println(sarr);//askcbd

        Arrays.sort(sarr);//返回void
        System.out.println(sarr);//abcdks

        String s2 = new String(sarr);
        System.out.println(s2);//abcdks

        String s3 = String.valueOf(sarr);
        System.out.println(s3);//abcdks
    

开发者涨薪指南 48位大咖的思考法则、工作方式、逻辑体系

以上是关于Leetcode 242. Valid Anagram-判断是否是 相同字母异序词的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 242 Valid Anagram

242. Valid Anagram(leetcode)

[leetcode-242-Valid Anagram]

[leetcode]242.Valid Anagram

LeetCode 242 Valid Anagram

LeetCode242——Valid Anagram