StringSegment

Problem Statement

 A string is considered to be a composite of several maximal segments of identical characters. For example, "aaabbaaac" is a composite of the segments: "aaa", "bb", "aaa" and "c". Given a string, return the average length of all the segments which form that string.
 

Definition

 
Class:StringSegment
Method:average
Parameters:String
Returns:double
Method signature:double average(String s)
(be sure your method is public)
 
 
 

Notes

-A return value with either an absolute or relative error of less than 1.0E-9 is considered correct.
 

Constraints

-s will contain between 1 and 50 characters, inclusive.
-s will contain only lowercase letters ('a'-'z').
 

Examples

0) 
 
"aaabbaaac"
Returns: 2.25
Average length of all the segments = ( 3 + 2 + 3 + 1 ) / 4 = 2.25
1) 
 
"aabbccdd"
Returns: 2.0
2) 
 
"abba"
Returns: 1.3333333333333333

package Test01;
public class Test01 {
	
public static void main(String args[])

{

	String a ="aaaaaa";
	System.out.println("res"+average(a));
	

}

	public static double average(String s)
	{
	 //int b =0; 
	 double count=1;
	 

	   for (int i=1;i<s.length();i++)
	   {
		   
		   if (s.charAt(i-1)!=s.charAt(i))
	        { 
			 	   
			 count++;
	        }
	       //b++;
	   }


	          int sum = s.length();
	          
	          
			  double res = sum/count;
			  System.out.println("sum"+sum);	
			  System.out.println("count"+count);
			  System.out.println("res"+res);
			  return res;			  
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值