Skip to content

Instantly share code, notes, and snippets.

@aryan-dixit-26
Created January 24, 2021 05:56
Show Gist options
  • Save aryan-dixit-26/38ce6bca9fb88d4e4b71efb87919f714 to your computer and use it in GitHub Desktop.
Save aryan-dixit-26/38ce6bca9fb88d4e4b71efb87919f714 to your computer and use it in GitHub Desktop.
Answer2
import java.util.HashMap;
import java.util.Map;
class Question2 {
public static final int MAX_UNICODE = 65_535;
public static void main(String[] args) {
System.out.println(checkWhetherStringIsUnique("Competitive Programming"));
}
public static boolean checkWhetherStringIsUnique(String string) {
Map<Character, Boolean> characters = new HashMap<>();
Map<Character, Integer> frequency = new HashMap<>();
char [] str = string.toCharArray();
for(int i = 0; i < str.length; i++)
{
if(frequency.get(str[i]) == null)
{
frequency.put(str[i],1);
}
else
{
frequency.put(str[i],(frequency.get(str[i]) + 1));
}
}
for(int i = 0; i < str.length;i++)
{
if(str[i] != ' ')
{
if(frequency.get(str[i]) != 1)
{
characters.put(str[i],false);
}
else if(frequency.get(str[i]) == 1)
{
characters.put(str[i],true);
}
}
}
for(int i = 0; i < str.length;i++)
{
if(characters.get(str[i]) == false)
{
return false;
}
}
return true;
}
}
// Aryan Dixit
// 191500156
// SECTION G
// 35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment