Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to solve the problem of effective letter ectopic words by LeetCode

Shulou Source: shulou.com Published: 2022-06-01 13:37:17 10月06日 Update

This article mainly introduces LeetCode how to solve the problem of effective letter ectopic words, the article is very detailed, has a certain reference value, interested friends must read it!

1. Title

Valid letter ectopic words

two。 Description

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

Example 1:

Enter: s = "anagram", t = "nagaram"

Output: true

Example 2:

Enter: s = "rat", t = "car"

Output: false

3. First of all, the idea of length comparison, the difference is not followed by using an array of length 26 to represent the number of characters appearing in the string s plus the number of characters appearing in the string t minus 1 and finally judging whether the number of each lowercase letter is 0 or not. At this time, the traversal operation is mainly carried out, and the time complexity is 4. Implement public boolean isAnagram (String s, String t) {

/ / compare the length, but not if the length is different

If (s.length ()! = t.length ()) {

Return false

}

/ * an array of 26 is used to represent the number of occurrences of each character

* add 1 to the number of characters that appear in the string s

* the number of characters appearing in the string t minus 1

* finally determine whether the number of each lowercase letter is 0

, /

Int [] chaCount = new int [26]

For (int I = 0; I < s.length (); iTunes +) {

ChaCount [s.charAt (I) -'a'] + +

ChaCount [t.charAt (I) -'a']--

}

For (int item: chaCount) {

If (item! = 0) {

Return false

}

}

Return true

}

The above is all the content of the article "how to solve the problem of effective letter ectopic words in LeetCode". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Characters letters number string valid length problem difference content lowercase array times examples articles input output complexity two value interest Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Xiaomi vpn Microsoft Shulou Technology