String Formatting in Java Using Format Method

AmitDiwan
Updated on 04-Jul-2020 09:57:44

194 Views

Followimg is the code to implement String formatting in Java using % −Example Live Demopublic class Demo {    public static void main(String args[]){       String my_str = " sample.";       String concat_Str = String.format("This is a" + "%s", my_str);       String format_str_1 = String.format("The value is %.4f", 78.92367);       System.out.println(concat_Str);       System.out.println(format_str_1);    } }OutputThis is a sample. The value is 78.9237A class named Demo contains the main function. Here a string value is defined, which is used to format the string, by concatenating it to another variable. Similarly, a ... Read More

Min Cost Climbing Stairs in Python

Arnab Chakraborty
Updated on 04-Jul-2020 09:57:40

888 Views

Suppose there is a staircase, here the i-th step will be some non-negative cost value cost[i] assigned. When we pay the cost, we can either climb one or two steps. We have to find minimum cost to reach the top of the floor, and we also can either start from the step with index 0, or the step with index 1.So, if the input is like cost = [12, 17, 20], then the output will be 17, The cheapest position to start from step 1 as we have to pay that cost and go to the top.To solve this, we ... Read More

Find Smallest Letter Greater Than Target in Python

Arnab Chakraborty
Updated on 04-Jul-2020 09:55:28

1K+ Views

Suppose we have a list of sorted characters’ letters. This is containing only lowercase letters, now we have a target letter t, we have to find the smallest element in the list that is larger than the given target.And letters also wrap around. So, if the target is t = 'z' and letters = ['a', 'b'], the answer is 'a'.So, if the input is like ["c", "f", "j"], t = 'a', then the output will be 'c'.To solve this, we will follow these steps −l := 0r := size of letters - 1while l target, thenr := mid -1otherwise, ... Read More

Longest Word in Dictionary in Python

Arnab Chakraborty
Updated on 04-Jul-2020 09:55:08

553 Views

Suppose we have a list of words representing an English Dictionary, we have to find the longest word in the given word list that can be built one character at a time by other words in words. If there is more than one possible answer, then return the longest word with the smallest lexicographical order. If there is no answer, then return the empty string.So, if the input is like ["h", "he", "hel", "hell", "hello"], then the output will be "hello"To solve this, we will follow these steps −trie := a new mapDefine a function insert(). This will take wordnow ... Read More

Java Program for Bitonic Sort

AmitDiwan
Updated on 04-Jul-2020 09:50:29

286 Views

In Bitonic Sort, the comparision is in predefined sequence (Bitonic sequence), not dependent on the data to be sorted. Let us see an example for Bitonic Sort Java program −Example Live Demopublic class Demo{    void compare_swap(int my_arr[], int i, int j, int direction){       if ((my_arr[i] > my_arr[j] && direction == 1) || (my_arr[i] < my_arr[j] && direction == 0)){          int temp = my_arr[i];          my_arr[i] = my_arr[j];          my_arr[j] = temp;       }    }    void merge_vals(int my_arr[], int low, int cnt, int direction){ ... Read More

Design HashMap in Python

Arnab Chakraborty
Updated on 04-Jul-2020 09:50:24

2K+ Views

Suppose we want to design a HashMap without using any built-in hash table libraries. There will be different functions as follows −put(key, value) − This will insert a value associated with key into the HashMap. If the value already exists in the HashMap, update the value.get(key) − This will return the value to which the specified key is mapped, otherwise -1 when this map contains no mapping for the key.remove(key) − This will Remove the mapping for the value key if this map contains the mapping for the key.So, if the input is like After initialization, call the put and ... Read More

Design HashSet in Python

Arnab Chakraborty
Updated on 04-Jul-2020 09:48:10

4K+ Views

Suppose we want to design a HashSet data structure without using any built-in hash table libraries. There will be different functions like −add(x) − Inserts a value x into the HashSet.contains(x) − Checks whether the value x is present in the HashSet or not.remove(x) − Removes x from the HashSet. In case the value does not exist in the HashSet, do nothing.So, to test it Initialize the hash set, then call add(1), add(3), contains(1), contains(2), add(2), contains(2), remove(2), contains(2)., then the output will be true (1 is present), false (2 is not present), true (2 is present), false (2 is ... Read More

Binary Search Program Using Recursion in Java

AmitDiwan
Updated on 04-Jul-2020 09:47:14

7K+ Views

Following is the program for Recursive Binary Search in Java −Example Live Demopublic class Demo{    int rec_bin_search(int my_arr[], int left, int right, int x){       if (right >= left){          int mid = left + (right - left) / 2;          if (my_arr[mid] == x)          return mid;          if (my_arr[mid] > x)          return rec_bin_search(my_arr, left, mid - 1, x);          return rec_bin_search(my_arr, mid + 1, right, x);       }       return -1;    } ... Read More

Kth Largest Element in a Stream in Python

Arnab Chakraborty
Updated on 04-Jul-2020 09:44:48

589 Views

Suppose we want to design a class to find the kth largest element in a stream. It is the kth largest element in the sorted order, not the kth distinct element.The KthLargest class will have a constructor which accepts an integer k and an array nums, that will contain initial elements from the stream. For each call to the method KthLargest.add, will return the element representing the kth largest element in the stream.So, if the input is like k = 3, initial elements = [4, 5, 8, 2], then call add(3), add(5), add(10), add(9), add(4). , then the output will ... Read More

Java Program for Anagram Substring Search

AmitDiwan
Updated on 04-Jul-2020 09:44:12

244 Views

Following is an example for Anagram Substring Search in Java −Example Live Demopublic class Demo{    static final int max_val = 256;    static boolean compare_vals(char my_arr_1[], char my_arr_2[]){       for (int i = 0; i < max_val; i++)       if (my_arr_1[i] != my_arr_2[i])       return false;       return true;    }    static void search_subs(String my_pattern, String my_text){       int pat_len = my_pattern.length();       int txt_len = my_text.length();       char[] count_pat = new char[max_val];       char[] count_txt = new char[max_val];       for ... Read More

Advertisements