Posts

Showing posts from June, 2024

String compression TC = O(n)

Image
  class stringcompression {     public static String printstring ( String str ){         StringBuilder result = new StringBuilder ();         for ( int i = 0 ; i < str . length (); i ++){             Integer count = 1 ;             while ( i < str . length () - 1 && str . charAt ( i ) == str . charAt ( i + 1 )){                 count ++;                 i ++;             }             result . append ( str . charAt ( i ));             if ( count > 1 ){                 result . append ( count );             }         }         return result . toString ();     }     public static void main ( String [] args ) {         String str = "abbcdd" ;         str = printstring ( str );         System . out . print ( str );     } } Output :-

Convert first letter to capital letter in java

  import javax . xml . transform . stax . StAXSource ; class firstupper {     public static String capital ( String str ){         StringBuilder strbuild = new StringBuilder ();         char ch = Character . toUpperCase ( str . charAt ( 0 ));         strbuild . append ( ch );         for ( int i = 1 ; i < str . length (); i ++){             if ( str . charAt ( i ) == ' ' && i < str . length () - 1 ){                 strbuild . append ( str . charAt ( i ));                 i ++;                 strbuild . append ( Character . toUpperCase ( str . charAt ( i )));             }             else {                 strbuild . append ( str . charAt ( i ));             }         }         return strbuild . toString ();     }     public static void main ( String [] args ) {                 String str = "hi i am udit " ;         String capital = capital ( str );         System . out . println ( capital );     } }

Counting Vowels in java

  class vowels {     public static int findlowerCaseVowelCount ( String str ){         int len = str . length ();         int vowelCount = 0 ;             for ( int i = 0 ; i < len ; i ++){             char ch = str . charAt ( i );                 if ( Character . isLowerCase ( ch ) == true ){                 if ( ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ){                     vowelCount ++;                 }             }         }             return vowelCount ;     }         public static void main ( String [] args ) {         String s1 = "hello" ;         int count = findlowerCaseVowelCount ( s1 );         System . out . println ( count );     } }

Addition of characters using string biuilder TC=O(26) or O(n)

  class addchar {     public static void main ( String [] args ) {                 StringBuilder str = new StringBuilder ( "udit" );         for ( char ch = 'a' ; ch <= 'z' ; ch ++){             str . append ( ch );         }         System . out . println ( str );     } }

addition of characters in string TC= O(n^2);

  class characteradd {     public static void main ( String [] args ) {                 String str = "tony" ;         for ( char i = 'a' ; i < 'z' ; i ++){             str += i ;         }         System . out . println ( str );     } }

Largest String using lexicographic order (TC = O(x*n)) x= size of string ; n=total number of strings

  class largeststring {     public static void main ( String [] args ) {         String fruits [] = { "apple" , "Mango" , "banana" };         String largest = fruits [ 0 ];         for ( int i = 1 ; i < fruits . length ; i ++){             if ( largest . compareToIgnoreCase ( fruits [ i ]) < 0 ){                                 largest = fruits [ i ];             }         }         System . out . println ( largest );             } } Time Complexity :- O(x*n) x = size of largest string n= total nuymber of strings

shortest path using strings

  class shortest {     public static void main ( String [] args ) {         int x2 = 0 , y2 = 0 , x1 = 0 , y1 = 0 ;         //Time Complexity is O(n)         String str = "WNEENESENNN" ;         for ( int i = 0 ; i < str . length (); i ++){             if ( str . charAt ( i ) == 'W' ){                 x2 = x2 - 1 ;             }             if ( str . charAt ( i ) == 'E' ){                 x2 = x2 + 1 ;             }             if ( str . charAt ( i ) == 'N' ){                 y2 = y2 + 1 ;             }             if ( str . charAt ( i ) == 'S' ){                 y2 = y2 - 1 ;             }         }         int f = ( int ) Math . sqrt (( x2 - x1 ) * ( x2 - x1 ) + ( y2 - y1 ) * ( y2 - y1 ));         System . out . println ( "Shortest path is " + f    );             } }

palindrome of string Shraddha mams logic

import java . util .* ; class strings {     public static boolean palendrome ( String str ){         for ( int i = 0 ; i < str . length () / 2 ; i ++){             if ( str . charAt ( i ) != str . charAt ( str . length () - i - 1 )){                 return false ;             }         }         return true ;     }     public static void main ( String [] args ) {         String str = "racpdcar" ;         boolean val = palendrome ( str );         if ( val == true ){             System . out . println ( "String is palendrome" );         }         else {             System . out . println ( "String is not a palendrome" );         }     } }

Palindrome of string my logic

  import java . util .* ; class strings {     public static void main ( String [] args ) {         String str = "racpar" ;         String palendrome ;         int count = 0 , i = 0 ;         int start = 0 , end = str . length () - 1 ;         while ( i < end ){             if ( str . charAt ( i ) == str . charAt ( end )){                 count ++;                 end --;                 i ++;             } else {                 break ;             }         }         if ( count == str . length () / 2 ){             System . out . println ( "String is palendrome" );         }         else {             System . out . println ( "String is not palendrome" );         }     } }

Stair case traversal search

  import java . util .* ; class stairs {     public static void main ( String [] args ) {                 int m [][] = {{ 10 , 20 , 30 , 40 },                    { 15 , 25 , 34 , 45 },                    { 27 , 29 , 37 , 48 },                    { 32 , 33 , 39 , 50 }};                             int start = m [ 0 ][ m . length - 1 ], key = 37 ;         int i = 0 , j = m [ 0 ]. length - 1 , z = 1 ;         while ( start != key ){             // if(key==start){             //     System.out.print("key found at position "+i+" "+j);             //     z--;             //     break;             // }             if ( key < start ){                 j --;                 start = m [ i ][ j ];             }             else {                 i ++;                 start = m [ i ][ j ];             }         }         System . out . print ( "key found at position " + i + " " + j );     } }

Sum of two diagonal

  import java . util . Scanner ; import java . util .* ; class diagnol {     public static void main ( String [] args ) {                 Scanner s = new Scanner ( System . in );         int m [][] = new int [ 3 ][ 3 ], sum = 0 ;         // int sum=0;         System . out . println ( "Enter the matrix " );         for ( int i = 0 ; i < m . length ; i ++){             for ( int j = 0 ; j < m [ 0 ]. length ; j ++){                 m [ i ][ j ] = s . nextInt ();             }         }         //left diagnol sum         for ( int i = 0 ; i < m . length ; i ++){             for ( int j = 0 ; j < m [ 0 ]. length ; j ++){                 if ( i == j ){                     sum = sum + m [ i ][ j ];                 }             }         }         //right diagnol         for ( int i = 0 ; i < m . length ; i ++){             for ( int j = 0 ; j < m [ 0 ]. length ; j ++){                 if ( i + j == m . length - 1 ){                     if ( i != j

Spiral Matrix

  import java . util .* ; class spiral {     public static void printspiral ( int matrix [][]){         int startRow = 0 , startCol = 0 , endCol = matrix [ 0 ]. length - 1 , endRow = matrix . length - 1 ;         while ( startRow <= endRow && startCol <= endCol ){             // top             for ( int i = startCol ; i <= endCol ; i ++){                 System . out . print ( matrix [ startRow ][ i ] + " " );             }             //right             for ( int j = startRow + 1 ; j <= endRow ; j ++){                 System . out . print ( matrix [ j ][ endCol ] + " " );             }             //bottom             for ( int i = endCol - 1 ; i >= startCol ; i --){                 if ( startRow == endRow ){                     break ;                 }                 System . out . print ( matrix [ endRow ][ i ] + " " );             }             //left             for ( int j = endRow - 1 ; j >= startRow + 1 ; j

Count Sort

Image
  class counting {     public static void main ( String args []){         int arr [] = { 1 , 4 , 1 , 3 , 2 , 4 , 3 , 7 };         int n = arr . length ;         int max = Integer . MIN_VALUE ;         for ( int i = 0 ; i < arr . length ; i ++){             max = Math . max ( max , arr [ i ]);         }             int count [] = new int [ max + 1 ];         for ( int i = 0 ; i < n ; i ++){             count [ arr [ i ]]++;         }         System . out . println ( "Count array is " );         for ( int i = 0 ; i < n ; i ++)         System . out . print ( count [ i ]);         // { 0 , 2 ,1 ,2, 2, 0 ,0, 1}         System . out . println ( " " );         int z = 0 ;         for ( int i = 0 ; i < n ; i ++){             while ( count [ i ] > 0 ){                 arr [ z ] = i ;                 z ++;                 count [ i ]--;             }         }                 System . out . println ( "Final array is" )         for ( int

selection sort

  public class selection {     public static void main ( String [] args ) {                 int arr [] = { 5 , 4 , 3 , 2 , 1 };         int minpos = 0 ;         int n = arr . length ;         for ( int i = 0 ; i < n - 1 ; i ++){             minpos = i ;             for ( int j = i + 1 ; j <= n - 1 ; j ++){                 if ( arr [ minpos ] > arr [ j ]){                     minpos = j ;                 }             }             int temp = arr [ minpos ];             arr [ minpos ] = arr [ i ];             arr [ i ] = temp ;         }         for ( int i = 0 ; i < n ; i ++){             System . out . print ( arr [ i ]);         }     }     }

Bubble Sort

  public class bubble {     public static void main ( String [] args ) {         int arr [] = { 5 , 4 , 1 , 3 , 2 };         for ( int i = 0 ; i < arr . length - 1 ; i ++){             for ( int j = 0 ; j < arr . length - 1 - i ; j ++){                 if ( arr [ j ] < arr [ j + 1 ]){                     int temp = arr [ j ];                     arr [ j ] = arr [ j + 1 ];                     arr [ j + 1 ] = temp ;                 }             }         }         for ( int i = 0 ; i < arr . length ; i ++){             System . out . print ( arr [ i ]);         }             }     }

Rotated binary search

Image
  public class rotatedpractice {     public static int rotated ( int arr [], int target ){         int start = 0 , end = arr . length - 1 ;         while ( start <= end ){                         int mid = ( start + end ) / 2 ;             if ( arr [ mid ] == target ){                 return mid ;             }             if ( arr [ start ] <= arr [ mid ]){                 if ( target >= arr [ start ] && target < arr [ mid ]){                     end = mid - 1 ;                 }                 else {                     start = mid + 1 ;                 }             }             else {                 if ( target > arr [ mid ] && target <= arr [ end ]){                     start = mid + 1 ;                 }                 else {                     end = mid - 1 ;                 }             }         }         return - 1 ;     }     public static void main ( String [] args ) {         int arr [] = { 4 , 5 , 6 , 7 , 0 , 1 , 2