Unique Email – Kotlin -Code Challenge

Problem

Every valid email consists of a local name and a domain name, separated by the '@' sign. Besides lowercase letters, the email may contain one or more '.' or '+'.

  • For example, in "alice@leetcode.com""alice" is the local name, and "leetcode.com" is the domain name.

If you add periods '.' between some characters in the local name part of an email address, mail sent there will be forwarded to the same address without dots in the local name. Note that this rule does not apply to domain names.

  • For example, "alice.z@leetcode.com" and "alicez@leetcode.com" forward to the same email address.

If you add a plus '+' in the local name, everything after the first plus sign will be ignored. This allows certain emails to be filtered. Note that this rule does not apply to domain names.

  • For example, "m.y+name@email.com" will be forwarded to "my@email.com".

It is possible to use both of these rules at the same time.

Given an array of strings where we send one email to each email[i], return the number of different addresses that actually receive mails.

Example 1:

Input: emails = ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"]
Output: 2
Explanation: "testemail@leetcode.com" and "testemail@lee.tcode.com" actually receive mails.

Example 2:

Input: emails = ["a@leetcode.com","b@leetcode.com","c@leetcode.com"]
Output: 3

Simple Kotlin Solution

fun main(args:Array<String>){

        var result = numUniqueEmails(arrayOf("test.email+alex@leetcode.com","test.email.leet+alex@code.com"))
        printResult("Unique Email Count ", result.toString())
}

fun numUniqueEmails(emails: Array<String>): Int {
    var list=ArrayList<String>()

    for (email in emails){
        var localname=email.split("@")[0].split("+")[0].replace(".","")
        var domain=email.split("@")[1]
        if(!list.contains("$localname@$domain")){
            list.add("$localname@$domain")
        }
    }
    return list.size
}

Valid Parentheses – Code Challenge- Kotlin

Problem

Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

Given a string s containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘ and ‘]’, determine if the input string is valid.

An input string is valid if:

Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.

Example

Example 1:
Input: s = "()"
Output: true

Example 2:
Input: s = "()[]{}"
Output: true

Example 3:
Input: s = "(]"
Output: false

Example 4:
Input: s = "([)]"
Output: false

Example 5:
Input: s = "{[]}"
Output: true

Solution- Kotlin

fun main(args:Array<String>){

    var result= isValid("{[]}")
    printResult("Remove Element ",result.toString())
}

fun isValid(s: String): Boolean {

    var stack=Stack<String>()
    s.forEach {
        when(it.toString()){
            "{"->stack.push("}")

            "("->stack.push(")")
            "["->stack.push("]")
            else-> {
                if(stack.isEmpty()||stack.pop()!=it.toString()){
                    return false
                }
            }
        }

    }

    return stack.isEmpty()
}

Search Insert Position -Binary Search Kotlin

Problem

Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You must write an algorithm with O(log n) runtime complexity.

Example

Example 1:
Input: nums = [1,3,5,6], target = 5
Output: 2

Example 2:
Input: nums = [1,3,5,6], target = 2
Output: 1

Example 3:
Input: nums = [1,3,5,6], target = 7
Output: 4

Example 4:
Input: nums = [1,3,5,6], target = 0
Output: 0

Example 5:
Input: nums = [1], target = 0
Output: 0

Solution- Kotlin

fun main(args:Array<String>){

    var result= searchInsert(intArrayOf(1,3,5,6),5)
        printResult("searchInsert",result.toString())
}

//Binary Search
fun searchInsert(nums: IntArray, target: Int): Int {
    var start=0
    var end=nums.size-1

    while (start<=end){
        var mid=(start+end)/2
        if (nums[mid]>target){
            end=mid-1
        }else if(nums[mid]<target){
            start=mid+1
        }else{
            return mid
        }
    }
    return start
}

Length Of Last Word Kotlin

Problem


Given a string s consisting of some words separated by some number of spaces, return the length of the last word in the string.

A word is a maximal substring consisting of non-space characters only.

Example

Example 1
Input: s = "Hello World"
Output: 5
Explanation: The last word is "World" with length 5.

Example 2:
Input: s = "   fly me   to   the moon  "
Output: 4
Explanation: The last word is "moon" with length 4.

Example 3:
Input: s = "luffy is still joyboy"
Output: 6
Explanation: The last word is "joyboy" with length 6.

Solution- Kotlin

fun main(args:Array<String>){

    var result= lengthOfLastWord("   fly me   to   the moon  ")
}

//Binary Search
fun lengthOfLastWord(s: String): Int {
    var result =0
    for(i in s.length-1 downTo   0) {
        if ((s[i].toString() == " " && result == 0)) continue

        if (s[i].toString() == " " && result != 0) break
        result++
    }
    return result
    }