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
}

Remove Duplicate – Kotlin Solution

Problem

Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.

Example 1:

Input: head = [1,1,2]
Output: [1,2]

Example 2:

Input: head = [1,1,2,3,3]
Output: [1,2,3]


Solution- Kotlin

fun main(args:Array<String>){
    var node1=ListNode(1)
    var node2=ListNode(1)
    var node3=ListNode(2)
//    var node4=ListNode(3)
//    var node5=ListNode(3)
//    node4.next=node5
//    node3.next=node4
    node2.next=node3
    node1.next=node2
    var result = deleteDuplicates(node1)
}



// * Example:
var li = ListNode(5)
var v = li.`val`
//  Definition for singly-linked list.
class ListNode(var `val`: Int) {
    var next: ListNode? = null
}


fun deleteDuplicates(head: ListNode?): ListNode? {

    if (head?.`val`==null) return head

    var result=ListNode(head.`val`)
    var dummy:ListNode=result
    var headNode=head

    while (headNode!=null){
        if(dummy.`val`!=headNode.`val` ){
            dummy.next= ListNode(headNode.`val`)
            dummy=dummy.next!!
        }
        headNode=headNode.next


    }
    return result
}

Square root of x -Sqrt(x) – Kotlin

Problem

Given a non-negative integer x, compute and return the square root of x.

Since the return type is an integer, the decimal digits are truncated, and only the integer part of the result is returned.

Note: You are not allowed to use any built-in exponent function or operator, such as pow(x, 0.5) or x ** 0.5.

Example 1:

Input: x = 4
Output: 2

Example 2:

Input: x = 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since the decimal part is truncated, 2 is returned.

Solution- Kotlin

fun main(args:Array<String>){

        var result = mySqrt(2000000000)
        printResult("Square Root of 2000000000", result.toString())
}


//Using Binary Search
fun mySqrt(x: Int): Int {
    var ans=0
    if(x!=0){
        var start:Long=0
        var end:Long=x.toLong()
        while (start<=end){
            var mid:Long=(start+end)/2

            if(mid*mid<x){
                start=mid+1
                ans=mid.toInt()

            }else if(mid*mid>x){
                end=mid-1
            }else{
                ans=mid.toInt()
                break

            }
        }
    }
    return ans
}

Plus One – Code Challenge-Kotlin

Problem

You are given a large integer represented as integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0’s.

Increment the large integer by one and return the resulting array of digits.

Example

Example 1:
Input: digits = [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Incrementing by one gives 123 + 1 = 124.
Thus, the result should be [1,2,4].

Example 2:
Input: digits = [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.
Incrementing by one gives 4321 + 1 = 4322.
Thus, the result should be [4,3,2,2].

Example 3:
Input: digits = [0]
Output: [1]
Explanation: The array represents the integer 0.
Incrementing by one gives 0 + 1 = 1.
Thus, the result should be [1].

Example 4:
Input: digits = [9]
Output: [1,0]
Explanation: The array represents the integer 9.
Incrementing by one gives 9 + 1 = 10.
Thus, the result should be [1,0].

Solution- Kotlin

fun main(args:Array<String>){

    var result= plusOne(intArrayOf(3,2,3,9),)
        printResult("Plus One",result.contentToString())
}

fun plusOne(digits: IntArray): IntArray {
    var carry=1
    for (i in digits.size-1 downTo 0){
       digits[i]+=carry
        carry=digits[i]/10
        if(carry==0){
            break
        }
        digits[i]=digits[i]%10
    }
    if (carry==1){
        var result=IntArray(digits.size+1)
        result[0]=carry
        return result
    }
    return digits
}

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()
}