livepython.net

221. Python Program to Find the Longest Part Without Repeated Letters

HardPhase 3 · Challenge

This one takes a few steps. It uses two for loops, slicing, in and break. You know all of them. Try a short example on paper first.

Overview

This is a well-known interview question about strings. It trains you to check every part of a string in a careful order.

Problem Statement

Ayesha makes passwords from long words. Ayesha wants the longest part of a word in which no letter appears twice.

A part is a run of letters that sit next to each other in the word. For example, cab is a part of scabs, but sab is not, because in scabs no s sits right before the a.

Read a word. Find the longest part with no repeated letter. If two parts have the same length, take the one that starts first (more to the left).

Print two lines: Length: <number> and Part: <part>.

Examples

Example 1. Input abcabcbb gives:

Length: 3
Part: abc

Why: abc, bca and cab all have 3 different letters, and abc starts first. Every part with 4 letters has a repeat.

Example 2. Input pwwkew gives:

Length: 3
Part: wke

Why: wke has 3 different letters. pwke is not a part, because in pwwkew a second w sits between pw and ke.

Input

One line with one word: small letters from a to z, 1 letter or more. Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.

Output

Two lines: Length:, a space and the length, then Part:, a space and the part. The tests try 9 different inputs, also one letter and a word with no repeats at all.

Try it first. The solution opens when you solve this question, or after 3 tries with Submit.

Finish this chapter firstChapter 12 · Working With Strings
solution.py
# write your code here

EXPECTED OUTPUT

1Length: 3
2Part: abc

YOUR OUTPUT

Press Run to see your output here. Lines are compared with the expected output.