OneCompiler

find largest substring

162

Example heading with h2 size

Example heading with h3 size

Following is sample java code.

// abcaac

const findLargestStr = (str) =>{
  let largestLength = 0;
  let tempString = '';
  let obj={}
  let currentIndex = 0;
  
  
  for(let i =0; i<str.length; i++) {
    //console.log(str[i])
    
    if(!obj[str[i]]) {
      tempString += str[i]
      obj[str[i]] =i

    } else {
      if(currentIndex <str.length) {
        i=currentIndex+1
      }
      
            obj={}
      largestLength = tempString.length;
      tempString=''
    }
  }
  return largestLength
}

console.log(findLargestStr('abcfaacabcd'))

// def longest_substring_without_repeating_characters(s):
//   """
//   Finds the length of the longest substring in the given string without repeating characters.

  // Args:
  //   s: The input string.

  // Returns:
  //   The length of the longest substring without repeating characters.
  // """
  // start = 0
  // max_length = 0
  // used_chars = {}

  // for end in range(len(s)):
  //   if s[end] in used_chars:
  //     start = max(start, used_chars[s[end]] + 1)
  //   used_chars[s[end]] = end
  //   max_length = max(max_length, end - start + 1)

  // return max_length
  
//   function lengthOfLongestSubstring(str) {
//   let start = 0;
//   let maxLen = 0;
//   const charMap = new Map();

//   for (let end = 0; end < str.length; end++) {
//     const char = str[end];

//     // If the character is already in the map and its index is within the current window
//     if (charMap.has(char) && charMap.get(char) >= start) {
//       // Move the start pointer to the right of the repeated character
//       start = charMap.get(char) + 1;
//     }

//     // Update the character's index in the map
//     charMap.set(char, end);

//     // Calculate the current substring length and update maxLen if necessary
//     maxLen = Math.max(maxLen, end - start + 1);
//   }

//   return maxLen;
// }

// function lengthOfLongestSubstring(str) {
//   let start = 0;
//   let maxLen = 0;
//   const charMap = {}; // Use an object instead of a Map

//   for (let end = 0; end < str.length; end++) {
//     const char = str[end];
// console.log(char, charMap[char], start, end, charMap[char] >= start)
//     // If the character is already in the map and its index is within the current window
//     if (charMap[char] >= start) {
//       console.log(charMap)
//       // Move the start pointer to the right of the repeated character
//       start = charMap[char] + 1;
//     }

//     // Update the character's index in the map
//     charMap[char] = end;

//     // Calculate the current substring length and update maxLen if necessary
//     maxLen = Math.max(maxLen, end - start + 1);
    
//     console.log('ml',maxLen, end, start )
//   }

//   return maxLen;
// }

// // Example usage
// const str = "abcaebcbb";
// const result = lengthOfLongestSubstring(str);
// console.log("Length of the longest substring without repeating characters:", result); // Output: 3

// Example usage
// const str = "abcabcbbebfgh";
// const result = lengthOfLongestSubstring(str);
// console.log("Length of the longest substring without repeating characters:", result); // Output: 3