(Easy) Find the Difference - LeetCode

Description:

Given two strings s and t which consist of only lowercase letters.

String t is generated by random shuffling string s and then add one more letter at a random position.

Find the letter that was added in t.

Example:

Input:
s = "abcd"
t = "abcde"

Output:
e

Explanation:
'e' is the letter that was added.

Solution:

class Solution {
    public char findTheDifference(String s, String t) {
        
        
        
        for(int i =0; i<t.length();i++){
            
            if(Count_Char(s,t.charAt(i))!= Count_Char(t,t.charAt(i))){
                
                return t.charAt(i);
            }
            
        }
        
        return 'a';
    }
    
    int Count_Char(String s, Character ch){
        
        int count =0;
        
        for(int i = 0; i<s.length(); i++){
            
            if(s.charAt(i) == ch){
                
                count++;
                
            }
        }
        
        return count;
    }
}

猜你喜欢

转载自www.cnblogs.com/codingyangmao/p/11395380.html