Question

Python Programming Given an integer k and a string s, find the length of the longest...

Python Programming

Given an integer k and a string s, find the length of the longest substring that contains at most k distinct characters.

For example, given s = "abcba" and k = 2, the longest substring with k distinct characters is "bcb".

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I explain you the procedure through the given example: s= abcba and k=2

  1. in the first iteration we check the whole string "abcba", as it contains more than k distinct elements we continue
  2. in the second iteration we check the first 4 and the last 4: abcb and bcba , as both of these contain more than k distinct elements we continue
  3. in the third iteration we check the first 3 and the next 3 and then the next 3: abc , bcb , cba  , as bcb contains 2 distinct elements which is <=k, we have found our result.

we have used set data structure to find the number of distinct elements.

A set contains only unique elements i.e. if we add abcba to a set , the set will contain a,b,c

then by finding the length of the set we can find the number of distinct elements.

PROGRAM:

#asking user to enter string
st = input("Enter a string: ")
n=len(st)

#asking user to enter value of k
k= int(input("Enter value of k: "))

#flag variable
f=0

for i in range(1,n+1): #iterate from 1 to length of string

    for j in range(i):  #iterate from 0 to i

        if len(set(st[j:n-i+j+1]))<=k:   # if number of distinct elements in substring <=k we have found the result
            r=st[j:n-i+j+1]    #recuired substring
            f=1
            break
    if f==1:
        break
print("substring: "+r)

OUTPUT 1:

Enter a string: abcba
Enter value of k: 2
substring: bcb

OUTPUT 2:

Enter a string: aabacbebebe
Enter value of k: 3
substring: cbebebe

Add a comment
Know the answer?
Add Answer to:
Python Programming Given an integer k and a string s, find the length of the longest...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can I please get help with this? will upvote upon completion. Problem 2. The longest common...

    Can I please get help with this? will upvote upon completion. Problem 2. The longest common substring problem is to find the longest string that is a substring of two strings. The longest common substring of the strings "ABABC", and "ABCBA" is string "ABC" of length 3. A substrings of strings is a series of consecutive letters of s. For example, "ABA” is a substring of “ABABC", but "ABAC" is not a substring of "ABABC". Design an algorithm such that...

  • Using C/C++, implement a function to find the length of the longest substring without repeating characters...

    Using C/C++, implement a function to find the length of the longest substring without repeating characters for a given string. For example, the longest substring without repeating letters for “abcabcbb” is “abc”, and the length is 3. For string “bbbbb” the longest substring is “b”, with the length of 1.You can use either character array or string class to store the string.

  • Do in Python Problem 2 Assume s is a string of lower case characters. Write a...

    Do in Python Problem 2 Assume s is a string of lower case characters. Write a program that prints the number of times the string ' lol' occurs in s For example, if s= 'azclololegghakl', then your program should print S Number of times lol occu rs is: 2 Problem 3 Assume s is a string of lower case characters Write a program that prints the longest substring of s in which the letters occur in alphabetical order. For example,...

  • Please solve in Python. You would like to set a password for an email account. However,...

    Please solve in Python. You would like to set a password for an email account. However, there are two restrictions on the format of the password. It has to contain at least one uppercase character and it cannot contain any digits. You are given a string S consisting of N alphanumerical characters. You would like to find the longest substring of Sthat is a valid password. A substring is defined as a contiguous segment of a string. For example, given...

  • Python3: Write the function longestSubpalindrome(s), that takes a string s and returns the longest palindrome that...

    Python3: Write the function longestSubpalindrome(s), that takes a string s and returns the longest palindrome that occurs as consecutive characters (not just letters, but any characters) in s. so longestSubpalindrome("ab-4-be!!!") returns "b-4-b".If there is a tie, return the lexicographically larger value -- in Python, a string s1 is lexicographically greater than a string s2 if (s1 > s2). So: longestSubpalindrome("abcbce") returns "cbc", since ("cbc" > "bcb"). Note that unlike the previous functions, this function is case-sensitive (so "A" is not...

  • Lab2: Processing Strings Part#1 – Counting Vows Assume s is a string of lower case characters....

    Lab2: Processing Strings Part#1 – Counting Vows Assume s is a string of lower case characters. Write a program that counts up the number of vowels contained in the string s. Valid vowels are: 'a', 'e', 'i', 'o', and 'u'. For example, if s = 'azcbobobegghakl', your program should print: Number of vowels: 5 Part#2 – Counting Bobs Assume s is a string of lower case characters. Write a program that prints the number of times the string 'bob' occurs...

  • Given a string S that contains lowercase English letters representing different types of candies. A substring...

    Given a string S that contains lowercase English letters representing different types of candies. A substring of a string S is a string Ssub that occurs in S. For example, "twix" is a candy name which is the substring of "twtwixtwixnerdstwixnerds". Each candy costs 1 unit. You can pick some consecutive candies such that you can create a palindrome of length L by using some or all picked candies while the number of all possible cases K (all possible palindromes)...

  • Define a python function even(string) that accepts a python string of any length and returns a...

    Define a python function even(string) that accepts a python string of any length and returns a new string made of just the characters with even indexes For example even('Final exam') should return a string 'Fnlea'.

  • Python QUESTION A Below we have an assignment of a string to s. What is the...

    Python QUESTION A Below we have an assignment of a string to s. What is the index of the colon in s? You can use find method in Python to get the answer. The answer should be an integer. s = 'X-DSPAM-Confidence: 0.8475' __________ QUESTION B What did you type to get the answer above? State the exact Python code (codes like s.split(), len(s), print('hello')). ___________ QUESTION C Now extract only the substring 0.8475 from s with expression s[ ]....

  • Using python Print out the subset of the string defined by the two integer parameters in...

    Using python Print out the subset of the string defined by the two integer parameters in a descriptive string by using string formatting.    Arguments: a string : the string can be of any length two integers : the integers can be positive or negative Note: it can be assumed that the two parameters will be valid indices in the string    Return: None    >>> stringFormatting("Cookies are delicious", 3, 5) prints the line : "The characters in positions 3...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT