avatar

superorange0707

  • Homepage
  • Tag
  • Category
Home Leetcode-204-Valid Anagram
文章

Leetcode-204-Valid Anagram

Posted 2022-07-13
5~7 min read

Valid Anagram

leetcode: https://leetcode.com/problems/valid-anagram/

Description:

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.

Idea:

solution1: use sorted api

Code:

class Solution {
    public boolean isAnagram(String s, String t) {
         if (s.length() != t.length()) {
            return false;
         }
        char[] sa = s.toCharArray();
        char[] ta = t.toCharArray();
        Arrays.sort(sa);
        Arrays.sort(ta);
      //return new String(ta).equals(new String(sa));
        return Arrays.equals(sa,ta);
    }
}

**solution 2: **

Step1: use array(simple hash table) to record each alphabet and the corresponding occurrences

Step2: record first string with the array, and then decrease value of the each index when record the second string

Step3: if all element’s value is 0 in the array, it means two strings are anagram.

Code:

class Solution {
    public boolean isAnagram(String s, String t) {
        //if two string's length is not equal, return false
         if (s.length() != t.length()) {
            return false;
         }
        //create a array with the size is 26(alphabet is 26 letters)
        int[] result = new int[26]; 
        //record each letter and the corresponding times with increase
        for(char x:s.toCharArray()){
        //get index by calculate ASCII
            result[x - 'a'] += 1; 
        }
        //record the other string, with decrease
        for(char y:t.toCharArray()){
            result[y - 'a'] -= 1; 
        }
        //verify each element's value of the array
        for(int i:result){
            if(i!=0){
                return false;
            }
        }
        return true;
    }
}
 result[x - 'a']
 //calculate the ASCII value
Leetcode
Leetcode Hash
Share

Further Reading

Apr 23, 2025

283 - Move Zero

[283 - Move Zero] 🔗 LeetCode Link Problem Description Given an integer array nums, move all 0's to the end of it while maintaining the relative order

Apr 23, 2025

27 - Remove Element

[27 - Remove Element] 🔗 LeetCode Link Problem Description Given an integer array nums and an integer val, remove all occurrences of val in nums in-pl

Apr 23, 2025

26 - Remove Duplicates from Sorted Array

[26 - Remove Duplicates from Sorted Array] 🔗 LeetCode Link Problem Description Given an integer array nums sorted in non-decreasing order, remove the

OLDER

Leetcode-59-Spiral Matrix II

NEWER

Leetcode-202-Happy Number

Recently Updated

  • Migrating Jenkins SCM Using GitLab from Bitbucket: SCM URL Bulk Replacement
  • 283 - Move Zero
  • 27 - Remove Element
  • 26 - Remove Duplicates from Sorted Array
  • Migrating from Bitbucket to GitLab? Here’s how to keep your teams moving without missing a beat!

Trending Tags

Course two pointer Binary Tree Hash SQL Leetcode Error Recording Gitlab Bitbucket Devops

Contents