給定一個英文字串,寫一段程式碼找出字串中首先出現三次的英文字母。
阿新 • • 發佈:2018-12-09
問題描述:
給定一個英文字串,寫一段程式碼找出字串中首先出現三次的英文字母。
輸入描述:
輸入資料一個字串,包括字母,數字。
輸出描述:
輸出首先出現三次的英文字母
例項:
Have you ever gone shopping
輸出: e
public class Solution{ public static void main(String[] args) { String str = "Have you ever gone shopping"; System.out.println(firstThree(str)); } public static char firstThree(String str){ char[] data = str.toCharArray(); int[] hash = new int[256]; for(int i = 0;i<data.length;i++){ char c = data[i]; if((c>='A'&&c<='Z')||(c>='a' &&c<='z')){ hash[c]++; if(hash[c] == 3){ return c; } } } return '0'; } }