Java Substring Comparisons

Sort by

recency

|

1755 Discussions

|

  • + 0 comments

    String smallest = "";
    String largest = "";

        if(k<=s.length()){
             for (int i = 0; i<=s.length()-k; i++){            
                if (s.substring(i,i+k).compareTo(smallest)<0 || smallest == "")  smallest = s.substring(i,i+k);
                if(s.substring(i,i+k).compareTo(largest)>0) largest = s.substring(i,i+k);                
             }
        }    
        return smallest + "\n" + largest;
    
  • + 0 comments

    for(int i = 0; ( i + k )<= s.length(); i++){

            String texto = s.substring(i, i + k);
    
            if (texto.compareTo(smallest)<0 || smallest =="") {
                smallest = texto;
            } 
            if (texto.compareTo(largest)>0 || largest =="") {
                largest = texto;
            } 
    
        }
    
  • + 0 comments

    Code

    public static String getSmallestAndLargest(String s, int k) { String smallest = ""; String largest = "";

        smallest = s.substring(0, k);
        largest = s.substring(0, k);
    
        for (int i = 1; i <= s.length() - k; i++) {
            String sub = s.substring(i, i + k);
    
            if (sub.compareTo(smallest) < 0) {
                smallest = sub;
            }
            if (sub.compareTo(largest) > 0) {
                largest = sub;
            }
        }
    
        return smallest + "\n" + largest;
    }
    
  • + 0 comments

    this took too long

    import java.io.*;
    import java.util.*;
    import java.lang.*;
    public class Solution {
    
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            String smallest = "";
            String largest = "";
            String originalString = scan.nextLine();
            int substringLength = scan.nextInt();
            List<String> wordsList = new ArrayList<>();
    
            // Iterate through the string, extracting substrings of the desired length
            for (int i = 0; i <= originalString.length() - substringLength; i++) {
                String substring = originalString.substring(i, i + substringLength);
                wordsList.add(substring);
            }
            Collections.sort(wordsList);
            String firstWordOfFirstSentence = wordsList.get(0).split("\\s+")[0];
            String lastSentence = wordsList.get(wordsList.size() - 1);
            String lastWordOfLastSentence = lastSentence.split("\\s+")[lastSentence.split("\\s+").length - 1];   
            System.out.println(firstWordOfFirstSentence); 
            System.out.println(lastWordOfLastSentence);
        }
    }
    
  • + 0 comments

    Substring comparisons in Java are powerful but must be used carefully to avoid common pitfalls like index errors and unnecessary memory usage (in older versions). For readability and safety, prefer utility methods like startsWith(), endsWith(), or regionMatches() when appropriate. Betguru ID