Question

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.

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

#include <iostream>

#include<algorithm>

#include<string>

using namespace std;

void lengthOfLongestSubstring(string s) {

int i = 0, max = 0, offset = -1;

int arr[256];

string substr = "";

fill_n(arr, 256, -1);

for (i = 0; i < s.length(); i++) {

int si = (int)s[i];

offset = std::max(offset, arr[si]);

max = std::max(max, i - offset);

arr[si] = i;

}

substr = s.substr(0, max);

cout <<"the longest substring without repeating letters for \""<<s<<"\" is \""<< substr<<"\" and the length is "<< max<<endl;

}

int main()

{

cout << "Enter string ";

string s;

cin >> s;

lengthOfLongestSubstring(s);

return 0;

}

//output

Enter string abcabc the longest substring without repeating letters for abc abc. is ab Press any key to continue .. . and th

Add a comment
Know the answer?
Add Answer to:
Using C/C++, implement a function to find the length of the longest substring without repeating characters...
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
  • 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".

  • C PROGRAMMING Implement a function (using only #include library) that can encrypt/decrypt with a substitution cipher....

    C PROGRAMMING Implement a function (using only #include library) that can encrypt/decrypt with a substitution cipher. cipher_sub (a, b, c, d) a is the string that has the data that will be encrypted or decrypted b is the string that has the result of the encrypt/decrypt c is the code string used for our substitution cipher (27 entries plus '\0' character) d is an integer that will pass two constants defined as ENC (encrypt) or DEC (decrypt) --> The function...

  • 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...

  • 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...

  • Write a C function that receives a character array (string), and it will check the characters...

    Write a C function that receives a character array (string), and it will check the characters of the array to figure out whether the string has a given valid pattern. The required pattern will be given to you. In this function, for instance, you should check if the ascii code of an element of the character array is between a range. The ascii table of all the printable characters will be provided to you. See the last page of this...

  • 1 (continued) b. (20 points) Complete the function with the prototype and description below. void removeNonLetters...

    1 (continued) b. (20 points) Complete the function with the prototype and description below. void removeNonLetters (char estr); This function should remove all characters in string str except letters. Remember that, since the string is passed by address, changes made to str inside the function are seen outside the function. For example, if the string 1. ERCE.2160 Fall 2019" before calling this function, then after calling removeNonLetters (1). 51 - "EECEFall". Hint: to access part of a string (a substring)...

  • Question 8 A C-string is a sequence of characters terminated by the null character. True False...

    Question 8 A C-string is a sequence of characters terminated by the null character. True False D Question 9 What is the longest C-string that can be put into this C-string variable? char s[9]; There is not enough information to determine the 8th Question 10 D AC string variable is just an array of characters without the null character. True False Question 11 5 pts If you have already removed a character in char variable ch) from the input stream,...

  • 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...

  • C++ PROGRAMMING Implement a bool function whose header is given below. Inside the function, declare, ask,...

    C++ PROGRAMMING Implement a bool function whose header is given below. Inside the function, declare, ask, and get a character from the user. The function will return true if the character the user enters is NOT in either of the words, otherwise it will return false. Keep in mind that uppercase and lowercase letters are not the same. You will need to change both words and the character input by the user to the same case (upper or lower) -...

  • PLEASE CODE IN PYTHON Run-length encoding is a simple compression scheme best used when a data-set...

    PLEASE CODE IN PYTHON Run-length encoding is a simple compression scheme best used when a data-set consists primarily of numerous, long runs of repeated characters. For example, AAAAAAAAAA is a run of 10 A’s. We could encode this run using a notation like *A10, where the * is a special flag character that indicates a run, A is the symbol in the run, and 10 is the length of the run. As another example, the string KKKKKKKKKKKKKBCCDDDDDDDDDDDDDDDKKKKKMNUUUGGGGG would be encoded...

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
ADVERTISEMENT