Problem · String

Find Length of Longest Good Subsequence

Learn this problem
EasyWalmartOA

Problem statement

A good subsequence is obtained from a string by removing zero or more characters without changing the order of the remaining characters.

Its length m must be positive and even. The first m / 2 characters must all be equal to one another, and the last m / 2 characters must all be equal to one another. The character used in the two halves may be the same or different.

For example, "2222" and "333444" are good subsequences, while "222" and "234234" are not good subsequences themselves.

Return the length of the longest good subsequence of s. Return 0 when no positive even length subsequence exists.

Function

findLengthOfLongestGoodSubsequence(s: String) → int

Examples

Example 1

s = "2222"return = 4

The entire string is good. Its first half is "22" and its second half is also "22".

Example 2

s = "333444"return = 6

The entire string is good. All three characters in the first half are "3", and all three characters in the second half are "4".

Example 3

s = "222"return = 2

The full string has odd length, so it is not good. Removing any one character leaves "22", a good subsequence of length 2.

Example 4

s = "234234"return = 2

The full string is not good because neither half contains one repeated character. Any two characters form a good subsequence with one character in each half, but no good subsequence of length 4 exists.

More Walmart problems

drafts saved locally
public int findLengthOfLongestGoodSubsequence(String s) {
    // write your code here
}
s"2222"
expected4
checking account