Problem · String

Str with Longest Len

Learn this problem
MediumMicrosoftFULLTIMEOA
See Microsoft hiring insights

Problem statement

A prefix of a string S is any leading contiguous part of S. For example, the string "codility" has the following prefixes: "", "c", "co", "cod", "codi", "codil", "codili", "codilit", and "codility". A prefix of S is called proper if it is shorter 📏 than S.

A suffix of a string S is any trailing contiguous part of S. For example, the string "codility" has the following sufixes: "", "y", "ty", "ity", "lity", "ility", "dility", "odility" and "codility". A suffix of S is called proper if it is shorter than S.

Let's now write a func called strWithLongestLen(String S) in the editor 👉

Task of your func:

  • Find the len of the longest string that is both a proper suffix of S and a proper prefix of S.
  • : 𓏲🐋 ๋࣭  ࣪endless thanks, spike!˖✩࿐࿔ 🌊

    🐳 Source note (Jul 17, 2026): The source asks for the length as an integer, but the online signature and example outputs used strings. They now return integers. The judged core task matches the visible source at about 99%.

    Function

    strWithLongestLen(S: String) → int

    Examples

    Example 1

    S = "abbabba"return = 4
    The string abba is both a proper prefix and a proper suffix of S. Its length is 4, and there is no longer valid string.

    Example 2

    S = "codility"return = 0
    The only string that is both a proper prefix and a proper suffix is the empty string, whose length is 0.

    Constraints

  • 1 <= S.length() <= 1,000,000
  • String S consists only lower case english letters (a - z)
  • More Microsoft problems

    drafts saved locally
    public int strWithLongestLen(String S) {
      // write your code here
    }
    
    S"abbabba"
    expected4
    checking account