Find Length of Longest Good Subsequence
Updated
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) → intExamples
Example 1
s = "2222"return = 4The entire string is good. Its first half is "22" and its second half is also "22".
Example 2
s = "333444"return = 6The 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 = 2The 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 = 2The 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 interview questions
- Merge a List In Between Two PositionsONSITE INTERVIEW · Seen Nov 2026
- Merge Intervals from Start and End ListsONSITE INTERVIEW · Seen Nov 2026
- Palindrome Check Ignoring SymbolsONSITE INTERVIEW · Seen Nov 2026
- Largest Team Where Every Pair OverlapsOA · Seen Nov 2026
- House RobberOA · Seen Sep 2026
- Maximum Points by Deleting ElementsOA · Seen Sep 2026
- Distinct Bitwise-OR Scores of Increasing SubsequencesOA · Seen Sep 2026
- Best Meeting PointOA · Seen Sep 2026
- Minimum Cost to Make Array EqualOA · Seen Sep 2026
- Minimum Stress PathOA · Seen Aug 2026
Similar questions at other companies
- Minimum Operations to Reduce a Transaction LedgerVisa · OAVisa interview questions
- Maximum String BeautyBank of America · OABank of America interview questions
- Minimum Substrings Without Repeating CharactersMicrosoft · OAMicrosoft interview questions
- Maximize Palindromic WordsSquarepoint Capital · OASquarepoint Capital interview questions
- Viral Content BalancerTikTok · OATikTok interview questions
- Find Minimum Time RequiredAmazon · OA
- Merging PalindromesOld Mission · OA
- Largest Palindromic Number from Available DigitsMarshall Wace · OAMarshall Wace interview questions
- Worker Task Assignment Part 1 - Balance WorkloadStripe · ONSITE INTERVIEWStripe interview questions
- Reorderable Task Scheduler With CooldownWaymo · PHONE SCREENWaymo interview questions