Java Program To Find Longest Substring Without Repeated Character | Ashok IT

Поділитися
Вставка
  • Опубліковано 16 гру 2024

КОМЕНТАРІ • 42

  • @ashokit
    @ashokit  Рік тому

    🔥 Welcome to Ashok IT..!!
    👉 Register Here For Online Training : bit.ly/4dBYJbX
    👉 Follow us in Instagram : bit.ly/3jzEKl8
    👉 Follow Us in WhatsApp : bit.ly/49wCWje
    👉 Visit Our Website : ashokit.in/

  • @lakshmidevilakshmidevi3984
    @lakshmidevilakshmidevi3984 10 місяців тому +1

    Excellent explanation.Esay approach.Thank you.

    • @ashokit
      @ashokit  10 місяців тому

      You are welcome!

  • @ayushsingh3967
    @ayushsingh3967 3 роки тому +13

    Your code would get fail if input string is like s ="abcbdaac" because you'r replacing the i value with map.get(ch) but in this case i should be replaced as map.get(ch) -1 as it has to include character "c" when second b is get encountered.

    • @shikharrawat4388
      @shikharrawat4388 2 роки тому +2

      Even replacing it by map.get(ch) -1 doest yeild the proper output

    • @thiestmayank
      @thiestmayank Рік тому +2

      Ther's no problem with the code WRT to your input i.e "abcbdaac" I'm getting the correct output
      longest substring is : [c, b, d, a]
      longest substring length is : 4
      NOTE: based on your input ---> i = map.get(ch); duplicate 'b' found at 3rd index but from the String first time 'b' occurred at 1st index
      and due to i++ loop control will be increased to i=2 and at second index we have 'c'
      finally getting the output as expected.

  • @manishjoshi539
    @manishjoshi539 2 роки тому +3

    public static int findL(String s) {
    int l = s.length();
    int a_pointer = 0;
    int b_pointer = 0;
    int max = 0;
    HashSet hashSet = new HashSet();
    while (b_pointer < l) {
    if (!hashSet.contains(s.charAt(b_pointer))) {
    hashSet.add(s.charAt(b_pointer));
    b_pointer++;
    max = Math.max(hashSet.size(), max);
    } else {
    hashSet.remove(s.charAt(a_pointer)); //
    a_pointer++; // to iterate to next pointer since the current pointer is removed in the previous step
    }
    }
    return max;
    }

    • @kartikeyachoudhary5742
      @kartikeyachoudhary5742 Рік тому

      it's only good when you want an int type return value but this program won't be able to print the subarray string.

  • @vaibhavnirmal1
    @vaibhavnirmal1 2 роки тому +1

    Thank you sir this was amazing series on string!

  • @raghvendrasinghkushwah8293
    @raghvendrasinghkushwah8293 4 дні тому

    Really helpful :), thanks for this video.

    • @ashokit
      @ashokit  4 дні тому

      Please contact our admin team : 9985396677

  • @PBtelugu
    @PBtelugu 2 роки тому

    Super Logic sir

  • @kogulselvanathan4218
    @kogulselvanathan4218 3 роки тому +2

    Hi, sir while explaining use debugging mode so that it will easy to understand more clearly. Anyways, nice tutorial.

  • @maheshbabu9909
    @maheshbabu9909 3 роки тому +1

    wow ur awesome sir

  • @dhanushakian5910
    @dhanushakian5910 6 місяців тому

    import java.util.LinkedHashMap;
    import java.util.Map;
    class Solution {
    public static void lengthOfLongestSubstring(String s) {
    String longestSubstring = null;
    int lengthOfLongestSubstring = 0;
    Map map = new LinkedHashMap();
    char[] arr = s.toCharArray();
    int start = 0;
    for (int i = 0; i < arr.length; i++) {
    char ch = arr[i];
    if (map.containsKey(ch)) {
    start = Math.max(start, map.get(ch) + 1);
    }
    map.put(ch, i);
    if (i - start + 1 > lengthOfLongestSubstring) {
    lengthOfLongestSubstring = i - start + 1;
    longestSubstring = s.substring(start, i + 1);
    }
    }
    System.out.println(lengthOfLongestSubstring);
    System.out.println(longestSubstring);
    }
    public static void main(String[] args) {
    lengthOfLongestSubstring("rsdhghkhjdrsbng");
    }
    }
    proper working code for all string inputs

    • @ashokit
      @ashokit  2 місяці тому

      🔥 Please follow us in WhatsApp channel : bit.ly/49wCWje

  • @rahulsingh-jg9wt
    @rahulsingh-jg9wt 3 роки тому +1

    Thanks

  • @my_love_sanatan
    @my_love_sanatan 3 роки тому +1

    Map will clear whole values inside Map??

  • @RISHIKA_REDDY555
    @RISHIKA_REDDY555 Рік тому +1

    the code will fail for ip:abac it gives 2 as op but the correct ans is 3 beacuse bac is longest sub string with no repeated character ...in your code we are not taking previous character!!!!!!!!!!!!

    • @ashokit
      @ashokit  Рік тому

      Please Contact Our Admin Team:wa.me/+919985396677
      👉 Subscribe To Our UA-cam Channel: bit.ly/41IHJdj

    • @gauravpalkar3885
      @gauravpalkar3885 Рік тому

      This code is failing.

    • @AdityaKumar-ym3fy
      @AdityaKumar-ym3fy 9 місяців тому

      Kinda too time taking approach, but working
      String s1 = "abac";
      String longestStr = "";
      String currentStr = "";

      Set charSet = new LinkedHashSet();

      for(int i = 0; i < s1.length(); i++) {
      for(int j = i; j < s1.length(); j++) {
      char ch = s1.charAt(j);
      if( !charSet.contains(ch) ) {
      charSet.add(ch);
      }
      else if( charSet.contains(ch) )
      {
      currentStr = charSet.toString();
      if(currentStr.length() > longestStr.length()) {
      longestStr = currentStr;
      }
      charSet.clear();
      currentStr = "";
      break;
      }


      }
      }

      System.out.println(longestStr);

  • @NishantKumar0786
    @NishantKumar0786 2 роки тому

    nice logic

  • @rjoker60
    @rjoker60 2 роки тому +3

    else block is not clear , why are you getting index value and setting it to i

    • @srikanthchincholi481
      @srikanthchincholi481 Рік тому

      If char repeated we should clear it otherwise we may get chars repeated and here taking index to start freshly to check for substring

    • @gauravpalkar3885
      @gauravpalkar3885 Рік тому

      Even I got the same question ? It's not working fine.

    • @Anil--hs1fb
      @Anil--hs1fb 3 місяці тому

      @@gauravpalkar3885true

  • @tricksforsolving2804
    @tricksforsolving2804 4 місяці тому

    i am not able to understand what else part doing

    • @ashokit
      @ashokit  4 місяці тому

      please contact our admin team : +91 9985396677

  • @maheshmagarde2143
    @maheshmagarde2143 2 роки тому

    Sir I written same program it is giving wrong answer if I pass string "aabb" it rutering 1 insted of 2.....?

    • @prajwalgvaishnav4991
      @prajwalgvaishnav4991 Рік тому

      The answer is 1 only since it should be "without repeating the characters " . Here "aa" substring is invalid. The correct answer is "a"

    • @srihari4827
      @srihari4827 Рік тому

      answer is 2 which is ab

    • @gauravpalkar3885
      @gauravpalkar3885 Рік тому

      Yes it is giving wrong result. I changed the program and its now working fine.

  • @AbhishekSingh-h6s
    @AbhishekSingh-h6s 4 місяці тому

    Hi Sir,
    This code is not working:
    public static void main(String [] args) {
    lengthoflongestsubstring("java");

    }
    public static void lengthoflongestsubstring(String s) {
    String longestsubstring = null;
    int longestsubstringlength = 0;
    Map map = new LinkedHashMap();
    char [] arr = s.toCharArray();
    for (int i = 0; i < arr.length; i++) {
    char ch = arr[i];
    if (!map.containsKey(ch)) {
    map.put(ch, i);
    }
    else {
    i= map.get(ch);
    map.clear();
    }
    if (map.size()>longestsubstringlength) {
    longestsubstringlength = map.size();
    longestsubstring = map.keySet().toString();
    }
    }}

    • @ashokit
      @ashokit  4 місяці тому

      please contact our admin team : +91 9985396677

  • @RichaBehera129
    @RichaBehera129 Рік тому

    why we are removing the map

    • @ashokit
      @ashokit  Рік тому

      Please Contact Our Admin Team:wa.me/+919985396677
      👉 Subscribe To Our UA-cam Channel: bit.ly/41IHJdj

  • @akhouritushar
    @akhouritushar 10 місяців тому

    This code wont work for string ABCDEFGABEF

    • @ashokit
      @ashokit  Місяць тому

      🔥 Please follow us in WhatsApp channel : bit.ly/49wCWje