Friday, May 26, 2017

Add Digits

Problem statement

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
Could you do it without any loop/recursion in O(1) runtime?

Wednesday, May 24, 2017

Simple Angular Route

Create an angular component

  • Create a component.ts
  • Create an html file
  • Import angular core modules
  • Define metadata for the component using @Component
  • Create a new class,specify the properties of a class and export the class

Wednesday, May 17, 2017

Length of last Word


Problem Statement

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.
If the last word does not exist, return 0.
Note: A word is defined as a character sequence consists of non-space characters only.
Link to GitHub : Code

Given s = "Prathap Kudupu",
return 6.

Solution

  • The result  is the difference between the length of the string and and index of  empty space
     public static int optimum(String s)
 {
  return s.trim().length()-s.trim().indexOf(' ')-1;
 }

Reverse Vowels of a String

Problem Statement

Write a function that takes a string as input and reverse only the vowels of a string.

Link to GitHub :Code

Tuesday, May 16, 2017

Student Attendance Record I

Problem Statement

You are given a string representing an attendance record for a student. The record only contains the following three characters:
  1. 'A' : Absent.
  2. 'L' : Late.
  3. 'P' : Present.
A student could be rewarded if his attendance record doesn't contain more than one 'A' (absent) or more than two continuous 'L' (late).
You need to return whether the student could be rewarded according to his attendance record.

Link to GitHub:Code

Largest Uncommon Sequence 1


Problem Statement

Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings.
subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string.
The input will be two strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn't exist, return -1.
Link to GitHub Java: Code
Link to GitHub JavaScript : Code

Valid Palindrome


Problem Statement

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
Example:


"A man, a plan, a canal: Panama" is a palindrome.
"race a car" is not a palindrome.

Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.

Solution

Link to GitHub python:Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
def isPalindrome(s):
    head,tail=0,len(s)-1
    while head < tail:
        while head <tail and not s[head].isalnum():
            head+=1
        while head <tail and not s[tail].isalnum():
            tail-=1
        if s[head].lower() != s[tail].lower():
            return False
        head+=1; tail-=1
    return True
isPalindrome("bob bob")
Link to GitHub Java  :Code
Link to GitHub JavaScript:  Code


  • We need 2 pointers head and tail
  • Head is the initial position and tail is the last position in the array
  • Increment the header if we do not find valid character .Decrement the tail if we do not find the valid character.
  • Loop through the array till we find tail is greater than head
  • Return false if header character is not equal to tail
public static boolean get(String str)
 {
  //if string is empty return true
  if(str.isEmpty())
  {
   return true;
  }
  int head =0, tail=str.length()-1;
  //loop  through the strings to find if it is a valid palindrome
   while(head <= tail)
   {
    //Increment the header if the character isLetter or digit
    if(!Character.isLetterOrDigit(str.charAt(head))){
     head++;
    }
    //Decrement the tail if the character isLetter or digit
    else if(!Character.isLetterOrDigit(str.charAt(tail))){
     tail--;
    }
    else {
       if(Character.toLowerCase(str.charAt(head)) 
                                     !=Character.toLowerCase(str.charAt(tail)))
       {
        return false;
       }
       head++;
       tail--;
     
    }
   }
  return true;
 }