Perform range sum queries on string as per given condition | C # program for the above approach ; Function to perform range sum queries on String as per the given condition ; Initialize N by String size ; Create array [ ] A for prefix sum ; Iterate till N ; Traverse the queries ; Check if if L == 1 range sum will be A [ R - 1 ] ; Condition if L > 1 range sum will be A [ R - 1 ] - A [ L - 2 ] ; Driver Code ; Given String ; Given Queries ; Function call
using System ; using System . Collections . Generic ; class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void Range_sum_query ( String S , List < pair > Query ) { int N = S . Length ; int [ ] A = new int [ N ] ; A [ 0 ] = S [ 0 ] - ' a ' + 1 ; for ( int i = 1 ; i < N ; i ++ ) { A [ i ] = S [ i ] - ' a ' + 1 ; A [ i ] = A [ i ] + A [ i - 1 ] ; } for ( int i = 0 ; i < Query . Count ; i ++ ) { if ( Query [ i ] . first == 1 ) { Console . Write ( A [ ( Query [ i ] . second ) - 1 ] + " STRNEWLINE " ) ; } else { Console . Write ( A [ ( Query [ i ] . second ) - 1 ] - A [ ( Query [ i ] . first ) - 2 ] + " STRNEWLINE " ) ; } } } public static void Main ( String [ ] args ) { String S = " abcd " ; List < pair > Query = new List < pair > ( ) ; Query . Add ( new pair ( 2 , 4 ) ) ; Query . Add ( new pair ( 1 , 3 ) ) ; Range_sum_query ( S , Query ) ; } }