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 does javascript find the longest special sequence?

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

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you how javascript finds the longest special sequence of relevant knowledge points, the content is detailed, the logic is clear, I believe most people still know too much about this knowledge, so share this article for your reference, I hope you can get something after reading this article, let's take a look at it.

Topic description

Given two strings, you need to find the longest special sequence from the two strings. The longest special sequence is defined as follows: the sequence is the longest subsequence unique to a string (that is, it cannot be a subsequence of other strings).

The subsequence can be achieved by deleting some characters from the string, but the relative order of the remaining characters cannot be changed. The empty sequence is listed as a subsequence of all strings, and any string is a subsequence of itself.

Enter as two strings and output the length of the longest special sequence. If it does not exist, return-1.

Example:

Enter: "aba", "cdc"

Output: 3

Parsing: the longest special sequence can be "aba" (or "cdc")

Description:

Both strings are less than 100 in length.

The characters in the string contain only 'axiaxiangz'.

The idea of solving the problem

Label: the meaning of the topic is understood, but it is difficult to understand.

Unique means that only you have it, and the other string does not.

For example, let two string variables have the names an and b, respectively.

A = 'cased, b =' cd','cd' is unique to a, so the longest subsequence is listed as' cd', length 2.

A = 'cd', b =' cd', 'cd',' centering,'d' is in both strings, so there is no unique longest subsequence, returning-1

Through the analysis of examples, the following conclusions are drawn:

If the length of the two strings is not the same, the longer string itself cannot be a subsequence of the short string, just return its length

If the contents of two strings are equal, then their unique longest subsequence does not exist and returns-1

Code

Java version

Class Solution {

Public int findLUSlength (String a, String b) {

If (a.equals (b))

Return-1

Return a.length () > b.length ()? A.length (): b.length ()

}

}

JavaScript version

/ * *

* @ param {string} a

* @ param {string} b

* @ return {number}

, /

Var findLUSlength = function (a, b) {

If (a = = b)

Return-1

Return a.length > b.length? A.length: b.length

}; draw an explanation

Figure 1

Figure 2

These are all the contents of the article "how to find the longest special sequence in javascript". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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