Posts

Showing posts with the label Strings

Java code to Split a String into Maximum number of Balanced Strings

Image
  We are given a string which contains only characters 'R' and 'L'. A balanced string is one which has equal number of 'R' and 'L' characters. The problem statement is to find the maximum number of balanced strings which can be extracted from an input String. Here is a link to the leetcode problem, and we'll try to solve it today. /**   * Split the String into maximum number of Balanced Strings   *   * @author computengine.com   * @param s   * @return  */ public int balancedStringSplit(String s) { int countR = 0 , countL = 0 , count = 0 ; // loop through the input string for ( int i = 0 ; i < s . length ( ) ; i + + ) { // increment if 'R' found if ( s . charAt ( i ) = = 'R' ) countR + + ; // increment if 'L' found if ( s . charAt ( i ) = = 'L' ) countL + + ; // increment counter when Number of 'R' match Number of 'L', and reset...

3 Ways to find if a String contains a substring using Java (Bonus: Ignore spaces)

Image
So, you have a substring (or a word) which you have to find if its part of a sentence or not. We can do this in multiple ways in Java, depending upon which part of the sentence you are looking at, or if you want to include spaces in the search. Let's check out the ways we can do this: 1. Word can be anywhere in the sentence Or, the Substring can be anywhere in the String. If the word you are looking for can be in any part of the sentence, we can use contains() method.  As per java documentation: boolean java.lang.String.contains(CharSequence s) Returns true if and only if this string contains the specifiedsequence of char values. Let's check out the implementation. String baseString = "This is the String we are going to use" ; //any part of the string contains the substring System . out . println ( baseString . contains ( "we" ) ) ; System . out . println ( baseString . contains ( "hello" ) ) ; Output: true false 2. Word is at the start of the ...

6 ways to Concatenate Strings in Java (Bonus: Choose concatenation character)

Image
We need to deal with Strings in real time projects, and concatenation of Strings is a common and important functionality. There are multiple ways supported in Java to concatenate 2 or more Strings. Let's try to understand what are our options: 1. Using the '+' operator This is the easiest and the most commonly used option. We can simply use '+' between 2 Strings to concatenate them. String s1 = "Hello" ; String s2 = "World" ; System . out . println ( s1 + s2 ) ; Output: HelloWorld 2. Using concat() function Java has inbuilt function for concatenation. + operator is used for addition operations as well. If you want your code to be specific and readable, concat() function is an easy approach. String s1 = "Hello" ; String s2 = "World" ; System . out . println ( s1 . concat ( s2 ) ) ; Output: HelloWorld 3. Using StringBuilder class StringBuilder class has inbuilt methond append(), which we can use for concatenati...

4 ways to compare Strings in Java (Bonus: Lexicographical Compare)

Image
  Comparing two Strings is a very common project requirement. It can be utilized in multiple ways.  The comparison can be done to verify that the output is as expected.  It can be utilized to validate a test case. There can be a functionality which depends on a certain input, and we can write conditions on the basis of input given. We have to compare in order to verify the correct input and direct to correct condition. So, we can say that String comparison is a very important functionality in Java.  Today we'll learn multiple ways to do String comparison. 1. Using Inbuilt function equals() The java documentation says : boolean java.lang.String.equals(Object anObject)  Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object. Let's check out an example: String s1 = "Newstring" ; String s2 = "NewString" ; String s3 = new...

Java Collections (HashMap) - Anagram Check of Two Strings

Image
  Continuing our streak with String related Java Interview Questions, we'll try to solve one of popular Interview questions for Java - find if two Strings are Anagrams . We'll be utilizing the Java Collections framework to solve this problem.  So, lets' begin! If you've not already checked Anagram definition from Wikipedia, Anagram is a Word or Phrase which is formed by re-arranging the letters of another word.  eg. GAINLY and LAYING are Anagrams, and so are FRIED and FIRED. So, if two Strings are Anagrams, the core concept is that, the letters that these Strings contain will be exactly the same, and will occur the same number of times! In order to solve this problem, we'll basically have to find out the unique letters that are occurring in a String, and on top of that, we'll have to find the number of occurrences of these letters.  Java Collections has a perfect answer to solve this problem -> Hashmaps We'll break down this problem into two parts. First...

Java Code to Check if String having Spaces is a Palindrome

Image
In one of our previous articles, we have written code to verify if a String is a Palindrome . We utilized the magic of For loops to Reverse a String and verify if its Palindrome.  Now, if the Interviewer decides to make the question a bit more difficult, the next step can be to verify that the Input String is having some Spaces, or special characters is a Palindrome or not, and still your code should be able to handle that. (Another Important Java Interview Question!) Its sounds difficult, but if you carefully look at the question, there is a simple trick to solve it. If we remove the extra characters and then compare the input string, we would be able to identify if the string is a Palindrome.  So let's get started! This is our base code to if a  String is a Palindrome .  Reversing the String is achieved in another function  Reverse a String . /**   * Check if String is palindrome   *   * @author computengine.com   * @param i...

Java Program to check Palindrome

Image
  Strings is such a favorite in Java Interviews, that you can expect at least one question on this topic. One of the important concept is Reversing a String , which we have covered earlier.  Logic can be applied using existing knowledge to solve a variety of questions. We'll see the same today.  A Palindrome is a String which if read in reverse order, is still the same. Eg. Radar, kayak etc.  See where we are coming at? So if you're asked to check if a String is a Palidrome, your knowledge of reversing a String can come in handy! So, we'll simply reverse a String, and compare if it matches our current String. That's it! For revision, our Reversal function was: /**   * Reverse a String using for loop   * @author computengine.com   * @param input   * @return  */ public String reverseString(String input) { System . out . println ( "Input String: " + input ) ; //Create a String with no data String output = "" ; //...