Thursday, December 8, 2022

LeetCode- challange + Java 8







14. Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings.

If there is no common prefix, return an empty string "".

 

Example 1:

Input: strs = ["flower","flow","flight"]
Output: "fl"

Example 2:

Input: strs = ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.

 

Constraints:

  • 1 <= strs.length <= 200
  • 0 <= strs[i].length <= 200
  • strs[i] consists of only lowercase English letters.

----------------------------------------------------------------------------------------------------------------

 Binary Tree 






----------------------------------------------------------------------------------------------------------

Example for Nullable class

package com.example.java8.Optional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class OptionalMain {

public static void main(String[] args) {

Customer customer_1 = new Customer(101, "Ali_1");
Customer customer_2 = new Customer(102, "Ali_2");
Customer customer_3 = new Customer(103, null);
Map<String, Customer> cusMap = new HashMap<>();
cusMap.put("Ali_1", customer_1);
cusMap.put("Ali_2", customer_2);
cusMap.put("Ali_3", customer_3);
String[] str = {"Ali_1", "Ali_2", "Ali_4"};
Arrays.stream(str).forEach(strA-> Optional.ofNullable(cusMap.get(strA)).ifPresent(cus->System.out.print(cus)));
}

}


OUTPUT:-

Customer [id=101, name=Ali_1]Customer [id=102, name=Ali_2]