Finding common characters in two strings

前端 未结 13 2502
终归单人心
终归单人心 2020-12-15 14:09

I am coding for the problem in which we got to count the number of common characters in two strings. Main part of the count goes like this

for(i=0; i < st         


        
13条回答
  •  有刺的猬
    2020-12-15 14:48

    No need to initialize and keep an array of 26 elements (numbers for each letter in alphabet). Just fo the following:

    1. Using HashMap store letter as a key and integer got the count as a value.
    2. Create a Set of characters.
    3. Iterate through each string characters, add to the Set from step 2. If add() method returned false, (means that same character already exists in the Set), then add the character to the map and increment the value.

    These steps are written considering Java programming language.

提交回复
热议问题