Java Substring Comparisons

Sort by

recency

|

1702 Discussions

|

  • + 0 comments

    import java.util.Scanner;

    public class Solution {

    public static String getSmallestAndLargest(String s, int k) {
        String smallest = "";
        String largest = "",s2;
        int j=0,s1=0,e=k;
        int n=s.length()-k+1;
        String []ss=new String[s.length()-k+1];
        while(j!=(s.length()-k+1)){
            ss[j]=s.substring(s1,e);
            s1++;
            e++;
            j++;
        }
        for(int i=0;i<n-1;i++){
            for(j=i+1;j<n;j++){
                if(ss[i].compareTo(ss[j])>0){
            s2=ss[i];
                    ss[i]=ss[j];
                    ss[j]=s2;
    
                }
            }
        }
        smallest=ss[0];
        largest=ss[n-1];
        return smallest + "\n" + largest;
    }
    
  • + 0 comments

    Why there is no other import statements? Do I need to solve this without using ArrayList, which was my approach...? I am using java 8 btw.

  • + 0 comments
    public class JavaSubstringComparisons {
        public static void main(String[] args) {
            System.out.println(getSmallestAndLargest("welcometojava", 14));
        }
    
        // Gets the smallest and largest substring with length k from a string s
        public static String getSmallestAndLargest(String s, int k) {
            if (k <= s.length()) {
                String smallestSubstring = s.substring(0, k);
                String largestSubstring = "";
    
                for (int i = 0; i <= s.length() - k; i++) {
                    String substring = s.substring(i, i + k);
                    if (substring.compareTo(smallestSubstring) < 0) {
                        smallestSubstring = substring;
                    }
                    if (substring.compareTo(largestSubstring) > 0) {
                        largestSubstring = substring;
                    }
                }
    
                return smallestSubstring + "\n" + largestSubstring;
            } else {
                return "Substring length cannot be greater than string length";
            }
        }
    }
    
  • + 0 comments

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    String ss;
    int k;
    int ct=0;
    Scanner sc=new Scanner(System.in);
    ss=sc.nextLine();
    k=sc.nextInt();
    String[] arr=new String[ss.length()-k+1];
    int st=0,ed=k-1;
    
        int m=0;
        while(m!=(ss.length()-k+1))
        {
    
            arr[m]=ss.substring(st,ed+1);
            st++;
            ed++;
            m++;
        }
        // for(int i=0;i<ss.length()-k;i++)
        // {
        //     System.out.print(arr[i]+" ");
        // }
        String small=arr[0];
        String large=arr[ss.length()-k];
        for(int i=0;i<ss.length()-k+1;i++)
        {
            if(small.compareTo(arr[i])<0)   small=arr[i];
        }
        for(int i=0;i<ss.length()-k+1;i++)
        {
            if(large.compareTo(arr[i])>0)   large=arr[i];
        }
        System.out.println(large+"\n"+small);
    }   
    

    }

  • + 0 comments

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc=new Scanner(System.in);
        String st=sc.next();
        int k=sc.nextInt();
    
        ArrayList<String> sub1=new ArrayList<>();
        for(int i=0;i<=st.length()-k;i++)
        {
            sub1.add(st.substring(i,i+k));
        }
    
        Collections.sort(sub1);
    
    
       System.out.println(sub1.get(0));
       System.out.println(sub1.get(sub1.size()-1));
    
    
    }
    

    }