Posts

Showing posts from July, 2023

Remove Element - Java

Problem : https://leetcode.com/problems/remove-element/description/  import java.util.Arrays ; public class test { public static void main (String[] args) { int nums[] = { 0 , 1 , 2 , 2 , 3 , 0 , 4 , 2 } ; int val = 2 ; int i= 0 ; for ( int j = 0 ; j<nums. length ; j++){ if (nums[j]!=val){ int temp = nums[i] ; nums[i] = nums[j] ; nums[j] = temp ; i++ ; } } System. out .println(i) ; System. out .println(Arrays. toString (nums)) ; } }

Valid Parentheses : Java #leetCodeAccepted

  Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. import java.util.Stack ; public class parenthesesCheck { public static void main (String[] args) { parenthesesCheck pc = new parenthesesCheck() ; System. out .println(pc.isValid( "]" )) ; } public Character getVal (Character ch){ Character c= ' ' ; if (ch== '}' ){ c= '{' ; } else if (ch== ']' ) { c= '[' ; } else if (ch== ')' ) { c= '(' ; } return c ; } public boolean isValid (String value){ String opening = "{([" ; String closing = "})]" ; Stack stc = new Stack() ; if (value.length()% 2 == 1 ){ return false; } for (Character ch:value.toCharArray()){ ...

Given two strings s and goal, return true if and only if s can become goal after some number of shifts on s - JAVA #leetcode

Given two strings  s  and  goal , return  true   if and only if   s   can become   goal   after some number of  shifts  on   s . A  shift  on  s  consists of moving the leftmost character of  s  to the rightmost position.   class Solution { public boolean rotateString ( String value , String goal ) { int count = 0 ; if ( value . equals (goal)){ count=count+ 1 ; } char arr []= new char [ value . length ()]; for ( int r = 1 ;r< value . length ();r++){ for ( int i = 0 ;i< value . length ()-r;i++){ arr[i]= value . toCharArray ()[i+r]; if (i<r){ arr[( value . length ())-(r-i)]= value . toCharArray ()[i]; } for ( int n = 0 ;n<r;n++){ arr[( value . length ())-(r-...

Rotating String Anticlockwise by N places - Java

another program :  Given two strings  s  and  goal , return  true   if and only if   s   can become   goal   after some number of  shifts  on   s . import java.util.Arrays ; public class testCode1 { public static void main (String[] args) { String value= "swati" ; String goal = "tiswa" ; testCode1 ts = new testCode1() ; boolean flag= false; for ( int i= 0 ; i<value.length()- 1 ; i++){ if (ts.rotateString(value , i).equals(goal)){ flag= true; break; } else { flag= false; } } if (flag){ System. out .println( "rotaion is found" ) ; } else { System. out .println( "rotaiton not found" ) ; } } public String rotateString (String value ,int rotate){ //value = "swati"; // azonam int val = value.length()- 2 ; //rotate=2; char arr...

longest common prefix - Java

#1 public class testCode1 { public static void main (String[] args) { String strs[] = { "flower" , "flower" , "flower" , "flower" } ; String min_len = strs[ 0 ] ; int min_len_word= 0 ; String prefix= "" ; if (strs. length == 1 ){ System. out .println(strs[ 0 ]) ;; } else { for ( int i= 0 ; i<strs. length ; i++){ if (strs[i].length()<min_len.length()){ min_len=strs[i] ; } } min_len_word=min_len.length() ; boolean flag = false; for ( int k= 0 ; k<min_len_word ; k++){ for ( int l= 1 ; l<strs. length ; l++){ if (strs[ 0 ].charAt(k)==strs[l].charAt(k)){ flag= true; } else { flag= false; break; } ...

Number is Prime or not -Python & Java

Python a= 31 flag = False for i in range ( 2 , a// 2 ) : if a%i== 0 : flag= True break if flag== False : print ( "number is prime" ) else : print ( "Not Prime" ) ----------------------------------------------------------------------------- Java public class test1 { static boolean isPrime ( int num){ if (num== 0 ||num== 1 ) return false; for ( int i= 2 ; i<num ; i++){ if (num%i== 0 ){ return false; } } return true; } public static void main (String[] args) { int num = 10 ; for ( int i= 0 ; i<num ; i++){ if ( isPrime (i)== true ){ System. out .println(i) ; } } } } public class test1 { public static void main (String[] args) { int num = 20 ; int flag= 0 ; for ( int i= 2 ; i<=num/ 2 ; i++){ if (num%i== 0 ){ System. ou...

Print only duplicate character in string - Python & Java

using lambda in java public class etst { public static void main (String[] args) { String text = "ramanrayat" ; HashMap<Character , Integer> hm = new HashMap<>() ; for (Character c: text.toCharArray()){ if (!hm.containsKey(c)){ hm.put(c , 1 ) ; } else { hm.put(c , hm.get(c)+ 1 ) ; } } hm.forEach((a , b)-> { if (b> 1 ) System. out .println(a+ "" +b) ; }) ; } } Java import java.util.HashMap ; import java.util.Iterator ; import java.util.Map ; public class test123 { public static void main (String[] args) { String text = "raman rayat" ; HashMap<Character , Integer> hm = new HashMap<>() ; for ( int i = 0 ; i<text.length() ; i++){ if (!hm.containsKey(text.charAt(i))) { if ((text.charAt(i)!= ' ' )){ hm.put(text.ch...

count duplicate character in string - Java & Python

 Python: a = "ramanrayat" d = {} for i in range ( len ( a ) ) : if ( a [ i ] not in d ) : d [ a [ i ] ] = 1 else : d [ a [ i ] ] =d [ a [ i ] ] + 1 for j in d: if d [ j ] > 1 : print ( j+ "," + str ( d [ j ] ) ) Java: import java.util.HashMap ; import java.util.Iterator ; import java.util.Map ; public class test123 { public static void main (String[] args) { String text = "raman rayat" ; HashMap<Character , Integer> hm = new HashMap<>() ; for ( int i = 0 ; i<text.length() ; i++){ if (!hm.containsKey(text.charAt(i))) { if ((text.charAt(i)!= ' ' )){ hm.put(text.charAt(i) , 1 ) ; } } else { hm.put(text.charAt(i) , hm.get(text.charAt(i))+ 1 ) ; } } for (Map.Entry<Character , Integer> item: hm.entrySet()){ char key =item.getK...

Return pair of number from integer array who's some is 5 (non repetive values) - Java & Python

Python  a = [ 1 , 3 , 2 , 4 , 0 , 1 ] d= {} for i in range ( len ( a ) - 1 ) : for j in range ( i+ 1 , len ( a ) ) : if a [ i ] +a [ j ] == 5 : if not ( a [ i ] in d and a [ j ] in d.values () or a [ j ] in d and a [ i ] in d.values () ) : d [ a [ i ] ] = a [ j ] print ( d ) Java import java.util.HashMap ; public class test123 { public static void main (String[] args) { int arr[] = { 1 , 3 , 4 , 1 , 0 , 2 } ; HashMap<Integer , Integer> hm = new HashMap<>() ; for ( int i= 0 ; i< arr. length ; i++){ for ( int j=i+ 1 ; j< arr. length ; j++){ if (arr[i]+arr[j]== 5 ){ if (!(hm.containsKey(arr[i])&& hm.containsValue(arr[j]) || hm.containsKey(arr[j])&& hm.containsValue(arr[i]))){ hm.put(arr[i] , arr[j]) ; System. out .println(arr[i]+ "" +arr[j]) ; } } ...

Reverse the integer - Java & Python

Pthon: a = 2356 num = 0 while ( a!= 0 ) : num = ( num* 10 ) + ( a% 10 ) a=a// 10 print ( num ) Java:   public class test1123 { public static void main (String[] args) { int a = 3221 ; int num= 0 ; while (a!= 0 ) { num = (num* 10 ) + (a % 10 ) ; a = a / 10 ; } System. out .println(num) ; } }

Rest Assured API Test - Java

  import io.restassured.RestAssured ; import io.restassured.path.json.JsonPath ; import io.restassured.response.Response ; import org.testng.annotations. Test ; import java.util.HashMap ; public class testAPI { Response res = RestAssured. given ().when().get( "https://reqres.in/api/users?page=2" ) ; @Test public void checkStatus (){ HashMap<String , String> hm = new HashMap<>() ; hm.put( "name" , "raman" ) ; hm.put( "job" , "tester" ) ; Response postcall = RestAssured. given ().contentType( "application/json" ).when().body(hm).post( "https://reqres.in/api/users" ) ; System. out .println(postcall.asString()) ; } @Test public void checkBody (){ JsonPath jsonpath = new JsonPath( res .asString()) ; System. out .println(jsonpath.get( "data[0].id" ).toString()) ; } }

Shift every character by 2 places ahead

  public class reverseStringFromPosition { public static void main (String[] args) { String s= "ramanrayat" ; int a= 0 ; String newString= "" ; for ( int i= 0 ; i<s.length() ; i++){ if (s.charAt(i)== 'y' || s.charAt(i)== 'z' ){ a = s.charAt(i)- 24 ; } else { a = s.charAt(i)+ 2 ; } newString=newString+( char )a ; } System. out .println(newString) ; } }

Rotate the String from position - Java

#1 public class reverseStringFromPosition { public static void main (String[] args) { String s= "ramanrayat" ; String rorateFrom = "y" ; System. out .println(rorateFrom+s.split( " y " )[ 1 ]+s.split( " y " )[ 0 ]) ; }     }   #2 public class reverseStringFromPosition { public static void main (String[] args) { String s= "ramanrayat" ; StringBuilder b= new StringBuilder(s) ; char [] cc ; for ( int i= 0 ; i<s.length() ; i++){ if (s.charAt(i)== 'y' ){ System. out .println(b.subSequence(i , s.length())+b.substring( 0 , i)) ; } } } }

Replace every third (3rd) duplicate character in string with dollor $- Java

  import java.util.HashMap ; import java.util.Iterator ; public class test1 { public static void main (String[] args) { StringBuilder abc = new StringBuilder( "dbbababtradd" ) ; System. out .println( "Input String :" +abc) ; HashMap<Character , Integer> hm = new HashMap<>() ; HashMap<Character , Integer> hmIndex = new HashMap<>() ; for ( int i = 0 ; i<abc.length() ; i++){ if (!hm.containsKey(abc.charAt(i))){ hm.put(abc.charAt(i) , 1 ) ; } else { hm.put(abc.charAt(i) , hm.get(abc.charAt(i))+ 1 ) ; if (hm.get(abc.charAt(i))== 3 ){ hmIndex.put(abc.charAt(i) , i) ; } } } Iterator itr = hmIndex.keySet().iterator() ; while (itr.hasNext()){ abc.setCharAt(hmIndex.get(itr.next()) , '$' ) ; } System. out .println( "Output...

Sort (Bubble) - Java

With 1 Loop  public class test123 { public static void main (String[] args) { int arr[] = { 1 , 5 , 2 , 8 , 12 , 3 , 44 } ; int temp ; for ( int i= 0 ; i<arr. length - 1 ; i++){ if (arr[i]>arr[i+ 1 ]){ temp = arr[i] ; arr[i]=arr[i+ 1 ] ; arr[i+ 1 ]=temp ; i=i- 1 ; } } for ( int k= 0 ; k<arr. length ; k++){ System. out .println(arr[k]) ; } } } Java: with 2 loops  public class test123 { public static void main (String[] args) { int arr[] = { 1 , 5 , 2 , 8 , 12 , 3 , 44 } ; int temp ; for ( int i= 0 ; i<arr. length ; i++){ for ( int j =i+ 1 ; j<arr. length ; j++){ if (arr[i]>arr[j]){ temp = arr[i] ; arr[i]=arr[j] ; arr[j]=temp ; } }...

Reverse The String Without Change the Position - Java

public class test1 { public static void main (String[] args) { String str= "Test the product" ; String arr[]=str.split( " " ) ; for ( int i= 0 ; i<arr. length ; i++) { for ( int j=arr[i].length()- 1 ; j>= 0 ; j--) { System. out .print(arr[i].charAt(j)) ; } System. out .print( " " ) ; } } }

Remove White Spaces From String - Java

Input : OneSpace TwoSpaces  ThreeSpaces   FourSpaces    Tab        End Output : OneSpaceTwoSpacesThreeSpacesFourSpacesTabEnd String stringWithoutSpaces = inputString.replaceAll("\\s+", ""); or      char[] charArray = inputString.toCharArray();                   String stringWithoutSpaces = "";                   for (int i = 0; i < charArray.length; i++)          {             if ( (charArray[i] != ' ') && (charArray[i] != '\t') )             {                 stringWithoutSpaces = stringWithoutSpaces + charArray[i];             }         }                   System.out.println("Input String : "+inputString);

Sort the String - Java

 #1 import java.util.Arrays ; public class test1 { public static void main (String[] args) { String text = "abcbdfq" ; char array[] = text.toCharArray() ; Arrays. sort (array) ; System. out .println(String. valueOf (array)) ; } } #2 public class test1 { public static void main (String[] args) { String text = "abcbaacd" ; char arr1[] = text.toCharArray() ; for ( int i= 0 ; i<text.length() ; i++){ for ( int j=i+ 1 ; j<text.length() ; j++){ if (arr1[i]>arr1[j]){ char temp = arr1[i] ; arr1[i]=arr1[j] ; arr1[j] = temp ; } } } System. out .println(String. valueOf (arr1)) ; } }

Remove Duplicate From String - Java & Python

PYTHON: a = "raman rayat" aa = "" for i in range ( len ( a ) ) : flag = False for j in range ( len ( aa ) ) : if a [ i ] ==a [ j ] : flag= True break if flag== False : aa=aa+a [ i ] print ( aa ) JAVA:  #1 using array public class test1 { public static void main (String[] args) { String s = "abcdabccdb" ; String s2 = "" ; for ( int i = 0 ; i < s.length() ; i++) { Boolean found = false; for ( int j = 0 ; j < s2.length() ; j++) { if (s.charAt(i) == s2.charAt(j)) { found = true; break; } } if (found == false ) { s2 = s2.concat(String. valueOf (s.charAt(i))) ; } } System. out .println(s2) ; } } #2 using linked hash set import java.util.LinkedHashSet ; import java.util.Set ; public class test1 { pub...