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

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ก.ย. 2024
  • #java #javascript #javaprogrammer #javaprogrammig #javadevelopment #ashokit
    ✍️✍️ Register Here For Online Training : bit.ly/4dBYJbX
    ** For Online Training ► Call: +91-6301921083
    Subscribe to our channel and hit the bell 🔔🔔🔔 icon to get video updates.
    💡 Visit Our Website
    For Online Training: www.ashokit.in
    💡 About Ashok IT :
    Ashok IT is the No.1 quality training institute in India for the candidates who want to build their future in Information Technology. We are into online training, class room training, corporate training and one to one training with more passion and dedication. Ashok IT aims in providing best quality realtime oriented trainings on C, C++, Java, Spring , Spring REST, Spring Cloud, Microservices, Python, DJango, .Net, Angular, React JS, Salesforce, , Testing, Android, Docker, Kubernates, Manual Testing, Selenium and Digital Marketing.
    -----------------------------------------------------------------------------------
    💡 Our Online Training Features
    🎈 Training with Real-time Working Professionals
    🎈 Industry Matching use cases
    🎈 Live Coding
    🎈 Real-time Environment
    🎈 Class Notes
    🎈 Doubts Clarifications in Each Session
    -----------------------------------------------------------------------------------
    💡 Contact details:
    ☎ WhatsApp Number: +91-6301921083
    ► Website : www.ashokit.in
    ► Join with us in Telegram : bit.ly/3iP2KyA
    ► Like us in Facebook : bit.ly/3dw6R0A
    ► Follow us in Instagram : bit.ly/3jzEKl8
    ► Follow us in Twitter : bit.ly/2SDqdK4

ความคิดเห็น • 38

  • @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/

  • @ayushsingh3967
    @ayushsingh3967 2 ปีที่แล้ว +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.

  • @lakshmidevilakshmidevi3984
    @lakshmidevilakshmidevi3984 7 หลายเดือนก่อน +1

    Excellent explanation.Esay approach.Thank you.

    • @ashokit
      @ashokit  7 หลายเดือนก่อน

      You are welcome!

  • @vaibhavnirmal1
    @vaibhavnirmal1 ปีที่แล้ว +1

    Thank you sir this was amazing series on string!

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

  • @rahulsingh-jg9wt
    @rahulsingh-jg9wt 3 ปีที่แล้ว +1

    Thanks

  • @kogulselvanathan4218
    @kogulselvanathan4218 3 ปีที่แล้ว +2

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

  • @my_love_sanatan
    @my_love_sanatan 2 ปีที่แล้ว +1

    Map will clear whole values inside Map??

    • @srikanthchincholi481
      @srikanthchincholi481 ปีที่แล้ว

      Yes, it should otherwise repeated chars will be strored

  • @maheshbabu9909
    @maheshbabu9909 3 ปีที่แล้ว +1

    wow ur awesome sir

  • @PBtelugu
    @PBtelugu 2 ปีที่แล้ว

    Super Logic sir

  • @tricksforsolving2804
    @tricksforsolving2804 28 วันที่ผ่านมา

    i am not able to understand what else part doing

    • @ashokit
      @ashokit  25 วันที่ผ่านมา

      please contact our admin team : +91 9985396677

  • @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 TH-cam Channel: bit.ly/41IHJdj

    • @gauravpalkar3885
      @gauravpalkar3885 8 หลายเดือนก่อน

      This code is failing.

    • @AdityaKumar-ym3fy
      @AdityaKumar-ym3fy 6 หลายเดือนก่อน

      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 ปีที่แล้ว

    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 8 หลายเดือนก่อน

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

    • @Anil--hs1fb
      @Anil--hs1fb 12 วันที่ผ่านมา

      @@gauravpalkar3885true

  • @dhanushakian5910
    @dhanushakian5910 2 หลายเดือนก่อน

    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

  • @AbhishekSingh-h6s
    @AbhishekSingh-h6s 25 วันที่ผ่านมา

    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  24 วันที่ผ่านมา

      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 TH-cam Channel: bit.ly/41IHJdj

  • @maheshmagarde2143
    @maheshmagarde2143 ปีที่แล้ว

    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 8 หลายเดือนก่อน

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

  • @akhouritushar
    @akhouritushar 6 หลายเดือนก่อน

    This code wont work for string ABCDEFGABEF