判断字符串有无重复字符

判断字符串有无重复字符

请实现一个算法,确定一个字符串的所有字符是否全都不同(有没有重复字符)。

public class Different
{

	public static void main(String[] args)
	{
		String str="beautiful";
		String str2="aABCDEFG";
		System.out.println(str+"  "+checkDifferent(str));
		System.out.println(str2+"  "+checkDifferent(str2));
		
	}
	 public static boolean checkDifferent(String str) {
		 if (str.isEmpty())return true;
		 int[] flag=new int[128];
		 //扫描字符串
		 for (int i = 0; i < str.length(); i++)
		{
			int c=(int)(str.charAt(i));
			if (flag[c]>0) return false;
			else {
				flag[c]++;
			}
		}
		 return true;
	 }
}

输出结果

在这里插入图片描述

发布了28 篇原创文章 · 获赞 6 · 访问量 494

猜你喜欢

转载自blog.csdn.net/weixin_43362002/article/details/104004756
今日推荐