Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    Can the text be split into words?

    MediumAbout 25 minFull AccessLearn it: Longest Increasing Subsequence and Word Break in C#: DP That Looks Back at Every Earlier Position

    The task

    A search engine for a language written without spaces checks whether a query can be read as a run of dictionary words.

    Complete CanSegment(string text, string[] words). Return true when text can be cut into pieces that are all in words. A word can be used any number of times, words are not empty, and matching is case-sensitive. The empty text can always be split, into no pieces.

    Signature

    public static bool CanSegment(string text, string[] words)

    Examples

    • "dotnetcore", ["dot", "net", "core"] → true. dot + net + core.
    • "carpet", ["car", "pe", "pet", "carp"] → true. car + pet; starting with the longer "carp" leaves "et", which is no word.

    Limits: Text up to 3,000 characters; up to 1,000 words of up to 50 characters.

    The tests check: the empty text, no words, a missing letter, words that overlap, a reused word, word lengths that must combine, letter case, and long texts with and without a split in time.

    Hints

      Your code

      C#Solution.csLoading the editor…

      Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;

      public static class Solution
      {
          // True when text can be cut into pieces that are all in words (words may repeat).
          public static bool CanSegment(string text, string[] words)
          {
              throw new NotImplementedException();
          }
      }

      This problem comes with TechInterviewNotes Full Access

      Unlock all paid content on TechInterviewNotes.