Longest Substring without repeating characters

Source: Internet
Author: User

Ask for the longest string, requiring all the letters in the string not to repeat the idea

Set head and tail two pointers, tail each move forward one cell, check whether all letters between tail and head are duplicated with the letter tail pointing, and if you repeat, point head to the next
For example:
ABCDEFDC, the current head points to A,tail to F, and when tail points to the next D, scans the value between head and tail, finds that D and tail that follow C are repeating, and then adjusts the head to point to E.

  
 
  1. class Solution {
  2. public:
  3. int lengthOfLongestSubstring(string s) {
  4. if (s == "")
  5. return 0;
  6. if (s.size() == 1)
  7. {
  8. return 1;
  9. }
  10. int head = 0, tail = 1, length = 1;;
  11. for (; tail < s.size(); tail++)
  12. {
  13. for (size_t j = head; j < tail; j++)
  14. {
  15. if (s[j] == s[tail])
  16. {
  17. head = j + 1;
  18. break;
  19. }
  20. }
  21. length = (tail - head+1)>length ? (tail - head+1) : length;
  22. }
  23. return length;
  24. }
  25. };


From for notes (Wiz)

Longest Substring without repeating characters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.