Posts

Showing posts from September, 2021

Java Output Formatting | Hackerrank

  Java's   System.out.printf   function can be used to print formatted output. The purpose of this exercise is to test your understanding of formatting output using   printf . To get you started, a portion of the solution is provided for you in the editor; you must format and print the input to complete the solution. Input Format Every line of input will contain a  String  followed by an  integer . Each  String  will have a maximum of   alphabetic characters, and each  integer  will be in the inclusive range from   to  . Output Format In each line of output there should be two columns: The first column contains the  String  and is left justified using exactly   characters. The second column contains the  integer , expressed in exactly   digits; if the original input has less than three digits, you must pad your output's leading digits with zeroes. Sample Input java 100 cpp 65 python 50 Sample Output ================================ java 100 cpp 065 python

Fibonacci Number (LeetCode)

  The   Fibonacci numbers , commonly denoted   F(n)   form a sequence, called the   Fibonacci sequence , such that each number is the sum of the two preceding ones, starting from   0   and   1 . That is, F(0) = 0, F(1) = 1 F(n) = F(n - 1) + F(n - 2), for n > 1. Given  n , calculate  F(n) .   Example 1: Input: n = 2 Output: 1 Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1. Example 2: Input: n = 3 Output: 2 Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2. SOLUTIONS (Java): class Solution { public int fib(int n) { int x=0, y=1, sum=0; if(n==0 || n==1) return n; else{ for(int i=2; i<=n;i++){ sum= x+y; x=y; y=sum; } } return sum; } }

China will fight six wars in the next 50 years!

 (The source of this content:  Tap Here  ) China is a big country without unity. This is the shame of the Chinese nation and the shame of the descendants of Yan and Huang.  For the unity of the country and the dignity of the nation, China must wage six wars in the next 50 years. It may be a nationwide war or a local war. But no matter which war, China must wage a united war.      The first war: reunification of Taiwan (2020-2025)     Although the two sides of the strait are becoming more and more peaceful, don't assume that the Taiwan's ruling authority (whether it is the KMT or the DPP) will peacefully reunify with the mainland. This is not in line with the ruling authority's campaign needs, so it will maintain the status quo with the mainland for a long time. The party is in favor, the DPP makes trouble, the Kuomintang and Yihe each obtain more political bargaining chips from it, "independence" dare not really "independence", make trouble, "unific

Longest Common Prefix (LeetCode)

  Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string  "" .   Example 1: Input: strs = ["flower","flow","flight"] Output: "fl" Example 2: Input: strs = ["dog","racecar","car"] Output: "" Explanation: There is no common prefix among the input strings. SOLUTIONS (Java): class Solution { public String longestCommonPrefix(String[] strs) { int n=strs.length,c=0; if(n==0||strs==null) return ""; Arrays.sort(strs); String first=strs[0], Last=strs[n-1]; while(c<first.length()){ if(first.charAt(c)==Last.charAt(c)) c++; else break; } return c==0?"":first.substring(0,c); } }

Count Items Matching a Rule (LeetCode)

  You are given an array   items , where each   items[i] = [type i , color i , name i ]   describes the type, color, and name of the   i th   item. You are also given a rule represented by two strings,   ruleKey   and   ruleValue . The  i th  item is said to match the rule if  one  of the following is true: ruleKey == "type"  and  ruleValue == type i . ruleKey == "color"  and  ruleValue == color i . ruleKey == "name"  and  ruleValue == name i . Return  the number of items that match the given rule .   Example 1: Input: items = [["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]], ruleKey = "color", ruleValue = "silver" Output: 1 Explanation: There is only one item matching the given rule, which is ["computer","silver","lenovo"]. Example 2: Input: items = [["phone","blue

Goal Parser Interpretation (LeetCode)

  You own a   Goal Parser   that can interpret a string   command . The   command   consists of an alphabet of   "G" ,   "()"   and/or   "(al)"   in some order. The Goal Parser will interpret   "G"   as the string   "G" ,   "()"   as the string   "o" , and   "(al)"   as the string   "al" . The interpreted strings are then concatenated in the original order. Given the string  command , return  the  Goal Parser 's interpretation of  command .   Example 1: Input: command = "G()(al)" Output: "Goal" Explanation:  The Goal Parser interprets the command as follows: G -> G () -> o (al) -> al The final concatenated result is "Goal". Example 2: Input: command = "G()()()()(al)" Output: "Gooooal" SOLUTION (Java): class Solution { public String interpret(String command) { String a=command.replace("()","o");