Posts

Showing posts from December, 2020

Strings-String Compression [codingblocks]

Take as input S, a string. Write a function that does basic string compression. Print the value returned. E.g. for input “aaabbccds” print out a3b2c2ds. Input Format A single String S. Constraints A string of length between 1 to 1000 Output Format The compressed String. Sample Input aaabbccds Sample Output a3b2c2ds Explanation In the given sample test case 'a' is repeated 3 times consecutively, 'b' is repeated twice, 'c' is repeated twice. But, 'd' and 's' occurred only once that's why we do not write their occurrence. Java Code: IDE Code:  https://ide.geeksforgeeks.org/btZOsVDRAd /* Amit Kumar 14-12-2020 IDE Code: https://ide.geeksforgeeks.org/btZOsVDRAd */ package String ; import java.util.Scanner ; public class Strings_StringCompression { public static Scanner scanner = new Scanner ( System . in ); public static void main ( String [] args ) { String str = scanner . next (); StringB...

CanYouReadThis? [codingblocks]

One of the important aspect of object oriented programming is readability of the code. To enhance the readability of code, developers write function and variable names in Camel Case. You are given a string, S, written in Camel Case. FindAllTheWordsContainedInIt. Input Format A single line contains the string. Constraints |S|<=1000 Output Format Print words present in the string, in the order in which it appears in the string. Sample Input IAmACompetitiveProgrammer   Sample Output I Am A Competitive Programmer   Explanation There are 5 words in the string.  JAVA code: IDE Code: https://ide.geeksforgeeks.org/qCrXFRPZ8x /* Amit Kumar 14-12-2020 IDE Code: https://ide.geeksforgeeks.org/qCrXFRPZ8x */ package String ; import java.util.Scanner ; public class String_CanYouReadThis { public static Scanner scanner = new Scanner ( System . in ); public static void main ( String [] args ) { String str = scanner . next (); i...

Strings-Max Frequency Character [codingblocks]

Take as input S, a string. Write a function that returns the character with maximum frequency. Print the value returned. Input Format String Constraints A string of length between 1 to 1000. Output Format Character Sample Input aaabacb   Sample Output a   Explanation For the given input string, a appear 4 times. Hence, it is the most frequent character.  JAVA Code: IDE Code: https://ide.geeksforgeeks.org/VUq5fnc859   /* Amit Kumar 14-12-2020 IDE Code: https://ide.geeksforgeeks.org/VUq5fnc859 */ package String ; import java.util.HashMap ; import java.util.Scanner ; public class Strings_MaxFrequencyCharacter { public static Scanner scanner = new Scanner ( System . in ); public static void main ( String [] args ) { String str = scanner . next (); int max = Integer . MIN_VALUE ; char maxCountChar = '\0' ; HashMap < Character , Integer > hashMap = new HashMap < Characte...

Strings-Difference in Ascii Codes [codingblocks]

Take as input S, a string. Write a program that inserts between each pair of characters the difference between their ascii codes and print the ans. Input Format String Constraints Length of String should be between 2 to 1000. Output Format String Sample Input acb   Sample Output a2c-1b   Explanation For the sample case, the Ascii code of a=97 and c=99 ,the difference between c and a is 2.Similarly ,the Ascii code of b=98 and c=99 and their difference is -1. So the ans is a2c-1b. Java Code: IDE Code: https://ide.geeksforgeeks.org/8QRMw1GBdz     /* Amit Kumar 14-12-2020 IDE Code: https://ide.geeksforgeeks.org/8QRMw1GBdz */ package String ; import java.util.Scanner ; public class Strings_DifferenceInAsciiCodes { public static Scanner scanner = new Scanner ( System . in ); public static void main ( String [] args ) { String str = scanner . next (); System . out . println ( stringDifferenceInAsciiCodes ( str )); ...

Strings-isPalindrome [codingblocks]

Take as input S, a string. Write a function that returns true if the string is a palindrome and false otherwise. Print the value returned. Input Format String Constraints String length between 1 to 1000 characters Output Format Boolean Sample Input abba   Sample Output true   Explanation A string is said to be palindrome if reverse of the string is same as string. For example, “abba” is palindrome as it's reverse is "abba", but “abbc” is not palindrome as it's reverse is "cbba". Java Code: IDE Code: https://ide.geeksforgeeks.org/3jrtNa4jPF     /* Amit Kumar 14-12-2020 IDE Code: https://ide.geeksforgeeks.org/3jrtNa4jPF */ package String ; import java.util.Scanner ; public class String_isPalindrome { public static Scanner scanner = new Scanner ( System . in ); public static void main ( String [] args ) { String str = scanner . next (); if ( isPalindrome ( str )) { System . out . println ...

Piyush and Magical Park [codingblocks]

Piyush is lost in a magical park which contains N rows and M columns.In order to get out of park safely and return home, he needs atleast K amount of strength.Given a N by M pattern, your task is to find weather Piyush can ever escape the park. Piyush enters the park with strength S. The park is filled with some obstacles denoted by ‘.’ , some magical beans denoted by ‘* ’ and some blockades denoted as ‘#’. If he encounters an obstacle (.) , strength decreases by 2. If he encounters a magic bean ('*   ') , his strength increases by 5. Piyush can only walk row wise, so he starts from left of a row and moves towards right and he does this for every row. However when he encounters a blockade (#) , he cannot go any further in his current row and simply jumps to the start of a new line without losing any strength. Piyush requires a strength of 1 for every step. His strength should always be greater than K while traversing or else Piyush will get lost. Assume that Piyush can shift im...