class Solution {
public int firstUniqChar ( String s) {
if ( s. length ( ) == 0 ) {
return - 1 ;
}
Map < Character , Integer > map = new HashMap < > ( ) ;
for ( int i = 0 ; i < s. length ( ) ; i++ ) {
char ch = s. charAt ( i) ;
map. put ( ch, map. getOrDefault ( ch, 0 ) + 1 ) ;
}
for ( int i = 0 ; i < s. length ( ) ; i++ ) {
if ( map. get ( s. charAt ( i) ) == 1 ) {
return i;
}
}
return - 1 ;
}
}
class Solution {
public int singleNumber ( int [ ] nums) {
HashSet < Integer > set = new HashSet < > ( ) ;
for ( int x: nums) {
if ( set. contains ( x) ) {
set. remove ( x) ;
} else {
set. add ( x) ;
}
}
for ( int i = 0 ; i < nums. length; i++ ) {
if ( set. contains ( nums[ i] ) ) {
return nums[ i] ;
}
}
return - 1 ;
}
}
class Solution {
public Node copyRandomList ( Node head) {
Map < Node , Node > map = new HashMap < > ( ) ;
Node cur = head;
while ( cur != null ) {
Node node = new Node ( cur. val) ;
map. put ( cur, node) ;
cur = cur. next;
}
cur = head;
while ( cur != null ) {
map. get ( cur) . next = map. get ( cur. next) ;
map. get ( cur) . random = map. get ( cur. random) ;
cur = cur. next;
}
return map. get ( head) ;
}
}
class Solution {
public int numJewelsInStones ( String jewels, String stones) {
Set < Character > set = new HashSet < > ( ) ;
int count = 0 ;
for ( int i = 0 ; i < jewels. length ( ) ; i++ ) {
char ch = jewels. charAt ( i) ;
set. add ( ch) ;
}
for ( int i = 0 ; i < stones. length ( ) ; i++ ) {
if ( set. contains ( stones. charAt ( i) ) ) {
count++ ;
}
}
return count;
}
}
import java. util. * ;
public class Main {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
while ( sc. hasNextLine ( ) ) {
String str1 = sc. nextLine ( ) ;
String str2 = sc. nextLine ( ) ;
func ( str1, str2) ;
}
}
public static void func ( String str1, String str2) {
Set < Character > set = new HashSet < > ( ) ;
for ( char ch1 : str2. toUpperCase ( ) . toCharArray ( ) ) {
set. add ( ch1) ;
}
Set < Character > set2 = new HashSet < > ( ) ;
for ( char ch2 : str1. toUpperCase ( ) . toCharArray ( ) ) {
if ( ! set. contains ( ch2) && ! set2. contains ( ch2) ) {
System . out. print ( ch2) ;
set2. add ( ch2) ;
}
}
}
}
class Solution {
public List < String > topKFrequent ( String [ ] words, int k) {
Map < String , Integer > map = new HashMap < > ( ) ;
for ( String s : words) {
if ( map. get ( s) == null ) {
map. put ( s, 1 ) ;
} else {
int val = map. get ( s) ;
map. put ( s, val+ 1 ) ;
}
}
PriorityQueue < Map. Entry < String , Integer > > minHeap = new PriorityQueue < > ( k, new Comparator < Map. Entry < String , Integer > > ( ) {
@Override
public int compare ( Map. Entry < String , Integer > o1, Map. Entry < String , Integer > o2) {
if ( o1. getValue ( ) . compareTo ( o2. getValue ( ) ) == 0 ) {
return o2. getKey ( ) . compareTo ( o1. getKey ( ) ) ;
}
return o1. getValue ( ) - o2. getValue ( ) ;
}
} ) ;
for ( Map. Entry < String , Integer > entry : map. entrySet ( ) ) {
if ( minHeap. size ( ) < k) {
minHeap. offer ( entry) ;
} else {
Map. Entry < String , Integer > top = minHeap. peek ( ) ;
if ( top. getValue ( ) . compareTo ( entry. getValue ( ) ) == 0 ) {
if ( top. getKey ( ) . compareTo ( entry. getKey ( ) ) > 0 ) {
minHeap. poll ( ) ;
minHeap. offer ( entry) ;
}
} else {
if ( top. getValue ( ) . compareTo ( entry. getValue ( ) ) < 0 ) {
minHeap. poll ( ) ;
minHeap. offer ( entry) ;
}
}
}
}
System . out. println ( minHeap) ;
List < String > list = new ArrayList < > ( ) ;
for ( int i = 0 ; i < k; i++ ) {
Map. Entry < String , Integer > top = minHeap. poll ( ) ;
list. add ( top. getKey ( ) ) ;
}
Collections . reverse ( list) ;
return list;
}
}