Showing posts with label String. Show all posts
Showing posts with label String. Show all posts

Saturday, May 18, 2019

How do you check palindrome?

Problem statement:
How do you check palindrome?
  1. public class Palindrome {
  2.     public static void main(String[] args) {
  3.         String s = "madam";
  4.         isPalindrome(s);
  5.     }
  6.     public static void isPalindrome(String s) {
  7.         String rev = "";
  8.         char c[] = s.toCharArray();
  9.         for (int i = s.length() - 1; i >= 0; i--) {
  10.             rev = rev + c[i];
  11.         }
  12.         if (s.equals(rev))
  13.             System.out.println("Yes");
  14.         else
  15.             System.out.println("No");
  16.     }
  17. }
Output:
Yes
Mehod-II:
  1. public class Palindrome {
  2.     public static void main(String[] args) {
  3.         String s = "madam";
  4.         System.out.println(isPalindrome(s) ? "Yes" : "No");
  5.     }
  6.     public static boolean isPalindrome(String s) {
  7.         int p = s.length() - 1;
  8.         boolean flag = true;
  9.         char c[] = s.toCharArray();
  10.         for (int i = 0; i < s.length() / 2; i++) {
  11.             if (c[i] != c[p]) {
  12.                 flag = false;
  13.                 break;
  14.             }
  15.             p--;
  16.         }
  17.         return flag;
  18.     }
  19. }
Output:
Yes

Tuesday, May 14, 2019

Java String Reverse

Problem statement:
A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward.
Given a string , print Yes if it is a palindrome, print No otherwise
Constraints:
A will consist at most 50 lower case english letters.
Sample Input:
madam
Sample Output:
Yes
  1. public class Palindrome {
  2.     public static void main(String[] args) {
  3.         String s = "madam";
  4.         System.out.println(isPalindrome(s) ? "Yes" : "No");
  5.     }
  6.     public static boolean isPalindrome(String s) {
  7.         int p = s.length() - 1;
  8.         boolean flag = true;
  9.         for (int i = 0; i < s.length() / 2; i++) {
  10.             if (s.charAt(i) != s.charAt(p)) {
  11.                 flag = false;
  12.                 break;
  13.             }
  14.             p--;
  15.         }
  16.         return flag;
  17.     }
  18. }
Output:
Yes
Method-II:
  1. public class Palindrome {
  2.     public static void main(String[] args) {
  3.         String s = "madam";
  4.         isPalindrome(s);
  5.     }
  6.     public static void isPalindrome(String s) {
  7.         String rev = "";
  8.         for (int i = s.length() - 1; i >= 0; i--) {
  9.             rev = rev + s.charAt(i);
  10.         }
  11.         if (s.equals(rev))
  12.             System.out.println("Yes");
  13.         elset a Palindrome");
  14.     }
  15. }
Output: 
Yes

Friday, May 10, 2019

How do you find longest common substring ?

Problem statement:
Given two strings S1 & S2. Find the longest common substring between S1 & S2.
  1. import java.util.ArrayList;
  2. import java.util.List;

  3. public class LongestCommonSubstring {
  4.     public static void main(String[] args) {
  5.         String S1 = "LCLC";
  6.         String S2 = "CLCL";
  7.         //String S3 = "abcdabccab";
  8.         //String S4 = "bcdaccabaabc";
  9.         List<String> com = commonSubstring(S1, S2);
  10.         for (String s: com){
  11.             System.out.println(s);
  12.         }
  13.     }
  14.     public static List<String> commonSubstring(String s1, String s2) {
  15.         Integer match[][] = new Integer[s1.length()][s2.length()];
  16.         int len1 = s1.length();
  17.         int len2 = s2.length();
  18.         int max = Integer.MIN_VALUE;    // max length of the string
  19.         ArrayList<String> result = null;    // result list
  20.         for (int i = 0; i < len1; i++) {    // row iteration
  21.             for (int j = 0; j < len2; j++) {  // column iteration
  22.                 if (s1.charAt(i) == s2.charAt(j)) {
  23.                     if (i == 0 || j == 0)
  24.                         match[i][j] = 1;
  25.                     else
  26.                         match[i][j] = match[i - 1][j - 1] + 1;
  27.                     // if you find a longer common substring re-initialize 
  28. // the max count and update the result list
  29.                     if (match[i][j] > max) {
  30.                         max = match[i][j];
  31.                         result = new ArrayList<String>();
  32.                         // substring starts at i-max+1 & ends at i
  33.                         result.add(s1.substring(i - max + 1, i + 1));
  34.                     }
  35.    // else if you find a common substring with the max length, 
  36.   // store it in the list
  37.                     else if (match[i][j] == max) {
  38.                         result.add(s1.substring(i - max + 1, i + 1));
  39.                     }
  40.                 } else {
  41.                     match[i][j] = 0;
  42.                 }
  43.             }
  44.         }
  45.         return result;
  46.     }
  47. }
Output:
LCL
CLC

Tuesday, April 23, 2019

How do you convert int to string using java?

Problem statement:
You are given an integer n, you have to convert it into a string.
If your code successfully converts into a string s the code will print "Good job". Otherwise it will print "Wrong answer".
n can range between 100 to 100 inclusive.
Sample Input 0
100
Sample Output 0
Good job

public class ConvertIntToString {
    public static void main(String[] args) {
        int n = 100;
        //String s = String.valueOf(n);    // method - 1
        //String s = "" + n;                    // method - 2
        String s = Integer.toString(n);     // method - 3
        if (n == Integer.parseInt(s)) {
            System.out.println("Good Job");
        } else {
            System.out.println("Wrong Answer");
        }
    }
}
Output:
Good Job

Saturday, April 13, 2019

Generate Parentheses

Problem statement:
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

For example, given n = 3, a solution set is:

[
  "((()))",
  "(()())",
  "(())()",
  "()(())",
  "()()()"
]

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...