hacker rank 16th problem solution

16 . java Substring comparison


//above are pre written... 

 public static String getSmallestAndLargest(String s, int k) {
        String   largest = "";
        String  smallest = ""; 
    int len ,str_len;
    
String tempMax = s.substring(0, k);
    String tempMin = s.substring(0, k);
    String temp2 = "";
    int temp2_int = 0;
    str_len =  s.length();

    for(int i = 0; i <= str_len; i++ ){
        len = i + k;
        if(len > str_len){
            break;
        }
        temp2 = (s.substring(i, len));
        temp2_int = tempMax.compareTo(temp2);
        if( temp2_int < 0 ){

            tempMax = temp2;
        }
        temp2_int = tempMin.compareTo(temp2);
        if(temp2_int > 0){
            tempMin = temp2;
        }

    }

    smallest = tempMin;
    largest = tempMax;
 
return smallest +"\n"+ largest;
}



Compiler Message
Success
Input (stdin)Download
welcometojava
3{-truncated-}
Download to view the full testcase
Expected OutputDownload

Comments

Popular posts from this blog

Amazon Web Services

Hacker Rank all java and python problem solutions

Deploying ML model to flask with joblib