Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-04-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report