Find the longest Substring of a given String S

import java.io.*;

import java.util.*;

  

public class GFG {

  

    

    

    public static void maxLength(int lis[], int n)

    {

        long or = 0;

  

        

        

        for (int i = 0; i < n; i++) {

            or |= lis[i];

        }

  

        int[] count = new int[32];

        for (int i = 0; i < n; i++) {

            for (int j = 0; j < 32; j++) {

                if ((lis[i] & (1L << j)) != 0)

                    count[j]++;

            }

        }

  

        long ans = 0, l = 0, tempOr = 0;

        for (int r = 0; r < n; r++) {

            tempOr |= lis[r];

  

            boolean flag = true;

            for (int i = 0; i < 32; i++) {

                if ((lis[r] & (1L << i)) != 0) {

                    count[i]--;

                    if (count[i] == 0)

                        flag = false;

                }

            }

  

            

            if (!flag) {

                for (; l <= r;) {

                    boolean flag1 = true;

                    for (int i = 0; i < 32; i++) {

                        if ((lis[(int)l] & (1L << i))

                            != 0) {

                            count[i]++;

                        }

                        if (count[i] == 0)

                            flag1 = false;

                    }

                    l++;

                    if (flag1)

                        break;

                }

            }

  

            

            

            if (tempOr == or) {

  

                ans = Math.max(ans, r - l + 1);

            }

        }

  

        

        if (ans != 0)

            System.out.println(ans);

        else

            System.out.println(-1);

    }

  

    

    public static void main(String[] args)

    {

        String S = "2347";

        int N = S.length();

        int[] A = new int[N];

        for (int i = 0; i < N; i++) {

            A[i] = S.charAt(i) - '0';

        }

  

        

        maxLength(A, N);

    }

}

Like this post? Please share to your friends:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: