Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to solve the problem of effective letter ectopic words in leetcode

Shulou Source: shulou.com Published: 2022-06-01 01:08:09 09月30日 Update

This article mainly shows you "how to solve the problem of effective letter ectopic words in leetcode". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "how to solve the problem of effective letter ectopic words in leetcode".

Topic link

Https://leetcode-cn.com/problems/valid-anagram/

Topic description

Given the two strings s and t, write a function to determine whether t is a letter heteronym of s.

Example 1:

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

Example 2:

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

Description: you can assume that the string contains only lowercase letters.

Advanced: what if the input string contains unicode characters? Can you adjust your solution to deal with this situation?

The idea of solving the problem

Tags: hash mapping

First, determine whether the two strings are equal in length, and return false directly if they are not equal.

If equal, initialize the 26-letter hash table and traverse the strings s and t

S is responsible for increasing at the corresponding position, and t is responsible for decreasing at the corresponding position.

If the value of the hash table is 0, then both are letter ectopic words.

Graphic illustration

Code class Solution {public boolean isAnagram (String s, String t) {if (s.length ()! = t.length ()) return false; int [] alpha = new int [26]; for (int I = 0; I < s.length (); iCode+) {alpha [s.charAt (I) -'a'] + Alpha [t.charAt (I) -'a']--;} for (int item0

Tags: Letters characters strings valid question content article hash input two location example topic study help output code function lowercase what to do Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi macOS Docker Shulou Tech Info Huawei