DSA Day 28/100

DSA Day 28/100

Topic: Strings

Questions Successfully Completed: 1

1) Sum of numbers in string

Easy

Sum of numbers in string

Time Complexity : O(N)

Space Complexity: O(N)

Question
Input: str = 1abc23 Output: 24 Explanation: 1 and 23 are numbers in the string which is added to get the sum as 24.
 public static long findSum(String str)
    {
        char[] ch = str.toCharArray();
        int num = 0;
        int sum = 0;
        for(int i=0;i<ch.length;i++){
            if(ch[i]>='0'&& ch[i]<='9')
            {
                num = (num*10) + (ch[i]-'0');
            }
            else{
                sum = sum + num;
                num = 0;
            }
        }
        return sum+num;
    }

Thank you for reading :)