Dynamic Programming: Basics and a Practical Example

Introduction: Dynamic programming is a problem-solving technique that can seem quite intimidating at first glance. However, once you grasp its fundamentals and see it in action, you'll find it to be a powerful tool in your coding arsenal. In this blog post, we'll demystify the basics of dynamic programming and walk through a practical example. We'll provide you with clear explanations and a complete Python program, so you can understand and apply this essential concept with confidence. Understanding Dynamic Programming: At its core, dynamic programming is all about breaking down a complex problem into smaller, overlapping subproblems. It's a technique used to efficiently solve problems by storing and reusing solutions to these subproblems, rather than recomputing them. This leads to significant improvements in runtime complexity. Example: Fibonacci Sequence with Dynamic Programming Let's start by tackling a classic problem: calculating Fibonacci numbers. The Fibonac...

HourRank 30 | Video Conference | Hackerrank

Source Hackerrank
Bob is making a video conference software. Whenever a new person joins the conference, Bob displays the person's name in the interface.
However, displaying full name is tedious and takes much space. So he decided to display the shortest prefix which doesn't match with any prefix of any person who has joined earlier.
Let's suppose the first person to enter the conference is alvin
=> a
Now suppose next person to join is alice. The shortest prefix of alice that doesn't match with any prefix of alvin is ali.
=> a
=> ali
If the full name of a new person matches completely with the full name of any person who has joined earlier, he will display the full name and add a suffix which indicates how many times the same name has occurred in the list so far. For example, if another person name alvin joins, the list will look like this:
=> a
=> ali
=> alvin 2
You are given the list of the persons who have joined the call in the chronological order. Your task is to figure out how the final list looks like.

Input Format
The first line contains an integer .
The subsequent  line contains a string  denoting the name of the  person to join the call.
Constraints
  •  will contain only lower-case english letters.
Subtask
  •  for  of the maximum score
Output Format
Return a string array with  items, the  line should contain the prefix of name of the  person which doesn't match with any other person who has joined earlier.
Sample Input 0
3
alvin
alice
alvin
Sample Output 0
a
ali
alvin 2
Sample Input 1
6
mary
stacy
sam
samuel
sam
miguel
Sample Output 1
m
s
sa
samu
sam 2
mi
Solution :
public static List<String> solve(List<String> names) {
        Map<String,Integer> data = new HashMap<>();
        
        List<String> result = new ArrayList<>();
        for(String name : names){
            
            boolean isPrefixFound = false;
            for(int i = 1; i < name.length(); i++){
                String prefix = name.substring(0,i);
                
                if(!isPrefixFound && !data.containsKey(prefix)){
                    isPrefixFound = true;
                    result.add(prefix);
                }
                
                if(!data.containsKey(prefix)){
                    data.put(prefix,0);
                }
            }
            
            int occures = data.getOrDefault(name,0);
            if(!isPrefixFound){
              String proccessed = (occures==0)?name:name + " " + (occures+1);
              result.add(proccessed);
            }
            data.put(name,occures+1);
        }
        return result;
    }

Comments

Popular posts from this blog

Array Transformation | biweekly-contest-12 | leetcode | Java Solution

Dynamic Programming: Basics and a Practical Example

leetcode | Longest Substring Without Repeating Characters Solution