instagram

Replace keys with valid values from sentence

This is the simple algorithm to replace all the keys from sentence with some values.

Here I am going to use StringBuilder to rebuild new string and It will going to take O(n), Here n is a number of characters in sentence.

Input :
String s = “Hi %NAME%, This is the great mail example, You got 10% discount on %ITEM%, Thanks for shopping with %COMPANY%,”

Here we have to replace three keys (%NAME% , %COMPANY% amd %ITEM%) with some values.

For example we have below map :

		map.put("NAME", "Dhiral Pandya");
		map.put("ITEM", "Computer");
		map.put("COMPANY", "omtlab");

Output :

Hi Dhiral Pandya, This is the great mail example, You got 10% discount on Computer, Thanks for shopping with omtlab,

Algorithm

Share